Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor " Watch "constructor " New topic
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor
 
Similar Threads
static and final keyword in constructors
mock
instance block is called first or constructior called first in java
class extension error
Which statement is true for default Constructor?