• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

about constructors

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



I'd love to help, but I'm having a hard time reading your code the way it is currently formatted.

If you would edit your post and use "code" tags around your code, that will preserve the indentations. Code that is properly indented is much easier to read!

- Jeff
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
1).First the object is allocated memory.To be clear,the instance variables are given default values.The instance variables are then intialised before the constructor call.
2)so it is possible to use the instance variables in the constructor.
In your code the String variable device in Mobile is not static.So u cannot access it with classname.Thats why the compiler complains.The constructor cannot be static.
Another thing...Static variable can hide the instance variable.
A) If the variable device in Mobile is declared static
Static variable device in Mobile hides the static variable device in Phone.
this.device in Phone class constructor refers Phone.device.It refers the variable in closer context.
this.device in Mobile class constructor refers Mobile.device.The same explanation holds good.i.e it refers the variabe in closer context.

B) If the device in Mobile is instance variable then this.device in phone constructor has the value null.This is because the variable is not initialised.so it has the default value.
Hope this helps...
 
Right! We're on it! Let's get to work tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic