• Post Reply Bookmark Topic Watch Topic
  • New Topic

Super class constructors  RSS feed

 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have a little doubt regarding a statement in K&B book...
The statement is -
"Only static variables and methods can be accessed as part of the call to super() or this()"
Please help me out...

Thanks
 
Narasimha Sharma
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are calling a constructor of a class using super() or this(), you don't have an object of that class yet.. So, you don't have access to instance variables or methods of that class. So, while instantiating a class using super() or this(), you have access only to their static members since they are loaded when jvm loads the class.

For eg:

class sample
{

static int a=10;

int b=11;

sample(int x)
{
//Something
}

sample()
{
this(a); Is Correct !
this(b); Is Incorrect ! (Both inserted individually)
}

}
 
Manoj Kumar M
Greenhorn
Posts: 5
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Static methods and variables are loaded into stack memory while loading a class into jvm. The instance variables and methods will be loaded into heap memory only after creating the object instances.

So while calling super and this keywords the instance members will not be available. So only you are getting that error.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!