hi
1) i know that static codes are executed when the class is loaded.
when will the instance or member variables get initialized is it b4 constructor get invoked or at the end of the constructor.
2) and also is possible to use the instance variable before creating instance i.e at the middle of constructor if it so how and why?
3) kindly go throug the code along the comments.
class Phone {
static
String device = "Phone.device";
void showDevice() {
System.out.println("Phone.showDevice," +
device + " ");
}
Phone() {
System.out.println("hai" + Phone.device);//working fine (1)
//System.out.println("hai" +Mobile.device);--->Throwing the error non-static cannot be referenced //from static context is that mean constructor is static
System.out.println("hai" + this.device);//the constructor is invoked from the Mobile then the o/p //should be Mobile.device but not so why??
showDevice();
}
}
class Mobile extends Phone {
String device = "Mobile.device";
void showDevice() {
System.out.println("Mobile.showDevice," + device + " ");
}
Mobile() {
//System.out.println("hai" + Mobile.device);//same error explained above.if (1)is working fine why //not this
System.out.println("hai" + Phone.device);//working fine.
System.out.println("hai" + this.device);
showDevice();
}
public static void main(String[] args) {
Mobile n = new Mobile();
n.showDevice();
}
}
i am sorry to post lot of questions but it is just to make ourself comfortable with the topics.
so please do reply to all the questions.