I have written a super class called Parent.java and it was extended to create a subclass called Child.java.
The super class can be compiled properly, but when the child is compiled an error is poped up.

//parent class
package abc;
public class Parent{
protected int st_marks=180;


//child class
package def;
import abc.Parent;
public class Child extends Parent{
protected double avg;
avg = st_marks/2;
public void display(){
System.out.println("Average ="+avg);


The error that I get when the child class is compiled is
Child.java:5: error: <identifier> expected
avg = st_marks/2;
1 error

Looking forward to receive a prompt respond.

Recommended Answers

All 6 Replies

It's nothing to do with Parent/Child. It's because you have an executable statement (avg = st_marks/2;) that's not inside a method or other executable block.
You could do that as an initialisation expression as part of the declaration of avg...

commented: Thanks +0

either you say:

protected double avg = avg = st_marks/2;

or you should set the value inside a constructor or method.

commented: Thanks +0

Hello Hans.

"Looking forward to receive a prompt respond."
2 hours later...
either it wasn't so urgent, or dlgmu537 isn't interested in communicating now he has his answer!

well, that's usually the case I guess...

its just package error and identifier .
you should have to put your abc package into def package.
because you are importing that package inside def.
also you have to put that line that you are doing some assignment in inline or inside function body.

As previously said:

protected double avg = st_marks/2;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.