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.

5 Years
Discussion Span
Last Post by jalpesh_007

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...

Votes + Comments

either you say:

protected double avg = avg = st_marks/2;

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

Votes + Comments

"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!


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;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.