Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructors

 
mukhan myrzakulov
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example : you can't do that
class A{

A{}
}

class B extends A{
void methB(){}
B{
methB();
super(); // must be the first sentence
}
}
 
priya shende
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thanks I appreciate it.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic