java - "Cannot reference Employee.DEFAULT_GENDER before supertype constructor has been called" Error -
i have following code
public class employee { private string name; private string gender; private int age; final string default_gender = "male"; final int default_age = 18; public employee(string name,string gender,int age) { this.name = name; this.gender = gender; this.age = age; } public employee(string name) { this(name,default_gender,default_age); } }
i getting following error
cannot reference employee.default_gender before supertype constructor has been called
i don't understand why saying employee.default_gender
? have not defined static! , why not allowing me call constructor 3 parameters? have defined default_gender
, default_age
ensure default values. need create employee object name(gender , age set default in case. no default constructor allowed). views of why happening?
default_gender
instance variable of class employee
, cannot used until instance of class created. until constructor executes instance not constructed, hence such error.
make both default values static
.
final static string default_gender = "male"; final static int default_age = 18;
qualifying variables static
makes them associated class employee
, hence can exist without creating instance of class employee
.
Comments
Post a Comment