File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Cannot use instance method/member until after super has run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Cannot use instance method/member until after super has run" Watch "Cannot use instance method/member until after super has run" New topic
Author

Cannot use instance method/member until after super has run

Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
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.


SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
Baiju Scariah
Ranch Hand

Joined: Mar 17, 2005
Posts: 33
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

Joined: Mar 01, 2005
Posts: 485
can u explain it with some example baiju
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298

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

Joined: Mar 17, 2005
Posts: 33
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

Joined: Dec 09, 2000
Posts: 214
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

Joined: Dec 09, 2000
Posts: 214
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

Joined: Jul 19, 2004
Posts: 298
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

Joined: Dec 09, 2000
Posts: 214
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot use instance method/member until after super has run