This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor

 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic