This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors" Watch "Constructors" New topic
Author

Constructors

mukhan myrzakulov
Greenhorn

Joined: Jan 16, 2008
Posts: 13
Hi, I couldn't understand that sentence:
"You cannot make a call to an instance method, or access an instance variable, until after the super constructor runs"

Can anybody explain it, with example would be great!
Thanks.
Antonio Tercero
Ranch Hand

Joined: Jun 05, 2008
Posts: 110
Example : you can't do that
class A{

A{}
}

class B extends A{
void methB(){}
B{
methB();
super(); // must be the first sentence
}
}


SCJP 5, SCWCD 5
priya shende
Greenhorn

Joined: Mar 17, 2008
Posts: 20
Hi Mukhan,
To access instance variable and instance method we need an object of that class.And whenever we create a object,constructor of that class is called.Any constructor implicity calls Parent class constructor through super(); statement.Which in return calls to its superparent class.
For example if we inherit class Animal in class Horse the inheritance tree will be like:

Object-->Animal-->Horse;

class Animal {
Animal(){
//super();
}
}
class Horse extends Animal
{
Horse(){
super(); //calls super class constructor
}

public static void main(String[] ar)
{
Horse d=new Horse(); //calls constructor
}
}



So when ever you will make Horse class object it will call constructor of Horse class,which in return will call Animal class Constructor,whether you supply one or not compliler will implcitly add one statement like super(); to invoke animal class constructor.And Animal class constructor will call Object class constructor.
First Object class constructor completes that Animal class constructor completes and then atlast Horse class constructor completes.When the horse class constructor completes than only object is created.And with the help of that object we access instance variable and instance method.So we conclusion is to access a object we need instance of a class And to create instance we need constructor,and construtor implicitly class parent class constructor.
Therefore
That's because you cannot invoke an instance (in other words, nonstatic)
method (or access an instance variable) until after the super constructor has run.


Priya
---------
SCJP(preparing);
mukhan myrzakulov
Greenhorn

Joined: Jan 16, 2008
Posts: 13
Got it. Thanks I appreciate it.
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
Hi,
I am still having doubt on this.

class A{

A{}
}

class B extends A{
void methB(){}
B{
super(); // must be the first sentence
this.methB(); / Added this
}
}
Once the super class constructor is called , then able to call instance variables or instance mathods.
But to call instance variables or methods we should have the constructor for that should be instantiated ( to call instance variables or instance methods we can use this).
If we call these in constructor , still the class is not instantaited. the this refers to what?

ram
Antonio Tercero
Ranch Hand

Joined: Jun 05, 2008
Posts: 110
Once the superconstructor is called, you can use this in any construtor or non-static method of your class.
this.variable means "the instance variable declared in this class"
this.method() means "the method declared in this class"
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
Thanks for your reply Antonio.
My doubt is that once called the super constructor , after that we can access the instance variables or instance methods in the constructor of the called class object .But still it's constructor is not completed. so where the instance variable value will be stored in memory( on behalf of this object part?)

rami
[ June 09, 2008: Message edited by: Ram Reddy ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors
 
Similar Threads
how can I print out the value of this list without havig to change evrything to static?
Interview Question
instance block is called first or constructior called first in java
This
non-static method cannot be referenced from a static context