public class Employee extends Person { private double salary;
public Employee(String firstName,String lastName,double salary) { super(firstName,lastName); this.salary=salary; } }
In the above code we will see the error message "cyclic inheritance involving Person
public class Person extends Employee"
Actually Person should be the super class here and Employee is the subclass. But here the relationsship is such that Employee is the subclass of Person, again Person is the subclass of Employee. It's a cyclic inheritance which is not possible.
The solution of this problem is identifying the proper relationship i.e, finding out the super class and the subclass. In our case Person is the super class, so it should not try to inherit Employee and Employee is the subclass which will inherit Person. Correction is ommiting "extends Employee" from the Person class signature as below:
public class Person { private String firstName; private String lastName;
public Person(String firstName,String lastName) { this.firstName=firstName; this.lastName=lastName; }
}
public class Employee extends Person { private double salary;
public Employee(String firstName,String lastName,double salary) { super(firstName,lastName); this.salary=salary; }
}
Read More
You see this error message because of any one of the followings:
1. You misspelled the keyword class (fully lower case) or you did not write it at all for writing your class.
public Class Test //actual keyword is class
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}
In the above example Class (C is in upper case) is written instead of class (fully lower case).
2. Remember you cannot declare variables outside of class body like below.
int variable; //variable must be declared inside the class body
public class Test
{
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}
To solve this declare variables inside the class body as below.
public class Test
{
int variable; //variable must be declared inside the class body
public void myMethod()
{
System.out.println("class, interface, or enum expected?");
}
}
3. All methods are also defined within the method body. If you define any method outside of the class body you will see this error message.
public class Test
{
public void myMethod()
{
System.out.println("method inside the class body");
}
} //end of class
public void anotherMethod()
{
System.out.println("method outside of the class body");
}
In the above code, the method anotherMethod is defined after the end of the class. So this error occurred. Most often it occurs just because of incorrect use of {} (curly braces). Check all { are closed with } in the proper place.
4. If you are defining an interface, check you spelled it correctly in fully lowercase.
public Interface Test //actual keyword is interface
{
public void myMethod();
}
In the above code, keyword interface is misspelled with upper I.
5. Same way if you define an enum spell the keyword enum correctly.
public Enum Day //actual keyword is enum
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
In the above example Enum is incorrect as the E is in upper case, it will be lower case e.
Read More
Very simple error ‘package system does not exist’- most probably you have used ‘err’ or ‘in’ or ‘out’ field of the ‘System’ class of ‘java.lang’ package but you have given lower case "s" instead of upper case "S" in the class name System. Remember Java is case sensitive.
See the below code:
system.out.println("package system does not exist error"); // here s is in lower case
But in the following code:
System.out.println("package system does not exist error solved");//S is in upper case
Though System is not a package, as you have written system.out.println – three parts here (system, out and println) a package like statement, Java compiler considers it as package but as this package doesn’t exist, the error message is shown.
So the solution is just change the lower case "s" of system to upper case "S".
Read More
If you try to access a non static member of a class from a static context, you will see ‘non-static method … cannot be referenced from a static context’. For example, you might have tried to call a non static method from a static method. Bear in mind, you can access a static member (variables, methods etc) from both static and non-static context but cannot access non-static member from a static context.
See the following example code:
public class NonStaticMethodCannotBeReferencedFromAStaticContext
{
private double firstNumber;
private double secondNumber;
private double result;
public void setFirstNumber(double firstNubmer)
{
this.firstNumber=firstNumber;
}
public void setSecondNubmer(double secondNumber)
{
this.secondNumber=secondNumber;
}
public double add()
{
return firstNumber+secondNumber;
}
public double substract()
{
return firstNumber-secondNumber;
}
public static void main(String args[])
{
setFirstNumber(10.35);// this is a non-static method but referenced from static main method
setSecondNubmer(20.97);
System.out.println("The addition is "+add());
}
}
For the solution you can:
i) declare the non-static member as static and access them from the static context (not always; be careful, this may create other problems)
ii) Create an instance of the class where the non-static members are and then reference the non-static members by the created instance or object.
Browse http://javatech-stuff.blogspot.com/2007/11/differences-between-java-terms.html for understanding the differences between static and non-static member.
Now see the solution below:
public class NonStaticMethodCannotBeReferencedFromAStaticContextSolved
{
private double firstNumber;
private double secondNumber;
private double result;
public void setFirstNumber(double firstNubmer)
{
this.firstNumber=firstNumber;
}
public void setSecondNubmer(double secondNumber)
{
this.secondNumber=secondNumber;
}
public double add()
{
return firstNumber+secondNumber;
}
public double substract()
{
return firstNumber-secondNumber;
}
public static void main(String args[])
{
NonStaticMethodCannotBeReferencedFromAStaticContextSolved object=new NonStaticMethodCannotBeReferencedFromAStaticContextSolved();
object.setFirstNumber(10.35);// accessed by the instance named object
object.setSecondNubmer(20.97);
System.out.println("The addition is "+object.add());
}
}
Read More
If a method is defined inside another method we may see ‘illegal start of expression’ error message. Remember that, we cannot define a method inside another method; we just can call a method inside another method. Sometimes it occurs just because of improper opening and closing of curly braces ( { or } ).
See the following example code:
public class IllegalStartOfExpression
{
public void methodA()
{
System.out.println("This is method A");
/* methodB is defined inside methodA because we didn’t close methodA before defining methodB*/
public void methodB()
{
System.out.println("This is method B");
}
}
}
Now see the solution:
public class IllegalStartOfExpression
{
public void methodA()
{
System.out.println("This is method A");
}
/* methodA is finished then, methodB is being defined*/
public void methodB()
{
System.out.println("This is method B");
}
}
Read More
This error message is shown when the statements are not written in proper place. Most often, mistakenly, we may write the processing task (for example assigning value to a variable; writing a loop etc.) outside of any method. In this situation we may see this type of error message.
Then the solution is to write such statements inside the appropriate methods.
See the example code below:
public class IdentifierExptected
{
int number1,number1,sum;
number1=10; // identifier expected
number2=20; // identifier expected
sum=number1+number2; // identifier expected
/*
The assignment statement must be written inside a method. Variables can also be assigned/ initialized during the declaration.
*/
public void display()
{
System.out.println("Number 1 = "+number1);
System.out.println("Number 2 = "+number2);
System.out.println("Sum = "+sum);
}
}
See the solution now:
public class IdentifierExptectedSolved
{
int number1=10,number2=20,sum;
public void sum()
{
sum=number1+number2;
}
public void display()
{
System.out.println("Number 1 = "+number1);
System.out.println("Number 2 = "+number2);
System.out.println("Sum = "+sum);
}
}
Read More
|