File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot use instance method/member until after super has run

 
Simon Cockayne
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

K&B page 316 says: "You cannot make a call to an instance methods, or access an instance variable, until after the super constructor runs".

Whilst this makes sense, since the first line of a constructor is explicitily a call to an ar/no-arg constructor or, implicitly this() or super(), how could one even try to call an instance method or access an instance variable before the super constructor runs?

I mean I get why you can't/shouldn't; I just don't see how you could even try?

Cheers,

Si.
 
Baiju Scariah
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ex: passing an instance variable when you create an object
- This gives an error because instance method or variables can be used only after super class run
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can u explain it with some example baiju
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I mean I get why you can't/shouldn't; I just don't see how you could even try?

Thats what the statement says. U cannot do this.
If it was u shouldnt do it, then ur question would have been a valid one, but the statement itself says u cannot do that ever.

like, the difference is here if i say
Simon cannot get less than 98% in SCJP , it means u cannot ever get less than 98%,
it doesnt mean u shouldnt get or u try not to get less than 98%.
 
Baiju Scariah
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
}

class B extends A
{
int num = 0;
B()
{
this(num);
}
B(int num)
{
super();
this.num = num;
}
public static void main(String args[])
{
new B();
}
}
 
Simon Cockayne
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Mmmm...

I understand that it cannot be done, as I have illustrated.

My point is, if it is not possible, then why is it highlighted as something you cannot do; presumably hihgtlight because someone might think that they can or should?

E.g. The book could say..."a constructor cannot be used to bring world peace".

Whilst this is a shame, and possibly true is it worth noting?

To get back to reality, with a more realistic example: One CANNOT modify a final variable after it has been initialised, but I could TRY to do that if I *wrongly* felt I SHOULD. Ergo, this is perhaps a good warning to say, you cannot.

See what I mean??

And let's not start raising people's expectations of me scoring 98% or higher...:-)


Cheers,

Si.
 
Simon Cockayne
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ha...Baiju has the answer,

Excellent example and well described.

Presumably this would also fail if I passed a call to in instance method as a parameter in a call to this.

Perfect.

Cheers,

Si.
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually if that statement wasnt given, take this code:

anyone might think that before the super constructor is called u can invoke the method add(),
So the book has specifically warned that it cannot be done.

Regarding ur expectations:

U expect 100 u get 100
U expect 80 u get 80

as in from the movie RAY

u think pennies u get pennies
u think dollars u get dollars
 
Simon Cockayne
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Animesh,

Indeed, I know see how one COULD attempt to access instance method or member before super has run, via an arugment to an overloaded constructor.

Now I seee that you CAN try, but that you SHOULD not, because it won't work.

Presently, I am full of glee and praise K&B for adding the caveat.

Darth Vader had high expectations and look what happened to him...

:-)

Cheers,

Si.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic