| Author |
constructor
|
harish shankarnarayan
Ranch Hand
Joined: Sep 12, 2005
Posts: 158
|
|
U cannot invoke an instance(non-static method or access an instance variable )until after the super constructor has run??? can anyone explain watdoes the above line mean with a simple piece of code
|
Harish<br />SCJP 1.4 (85%)
|
 |
Edisandro Bessa
Ranch Hand
Joined: Jan 19, 2006
Posts: 584
|
|
Hi harish, The affirmation is correct ! Any instance methods or properties cannot be accessed before its super constructor runs. In order to your class exist, its super classes must exist before. It
|
"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
|
 |
Edisandro Bessa
Ranch Hand
Joined: Jan 19, 2006
Posts: 584
|
|
Hi harish, The affirmation is correct ! Any instance methods or properties cannot be accessed before its super constructor runs. In order to your class exist, its super classes must exist before. It's a pre-requisite. Please take a look at the following code : So now, just changing the order for super() call the code will compile fine.
|
 |
harish shankarnarayan
Ranch Hand
Joined: Sep 12, 2005
Posts: 158
|
|
U cannot invoke an instance(non-static method or access an instance variable )until after the super constructor has run this is saying somrthing abt accessing the instance var or method before running the super class. i Didnt get ur point it says we can access static methods but
|
 |
Edisandro Bessa
Ranch Hand
Joined: Jan 19, 2006
Posts: 584
|
|
this is saying somrthing abt accessing the instance var or method before running the super class.
Yes, your instance methods or variables will be accessible only after super constructor has ran.
it says we can access static methods but
Concerning static methods and static variables, it's not related to new keyword. When you access a static member in your class, the class is already expected to be loaded by JVM. Don't confuse with constructor which is called when you creates a new class instance via new keyword. Anyway, the rules for access instance or static members within a constructor is the same. [B] You cannot use ANY member after super's constructor has run. If you still are confused please let me know.
|
 |
 |
|
|
subject: constructor
|
|
|