Forums Register Login

Cannot use instance method/member until after super has run

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
can u explain it with some example baiju
+Pie Number of slices to send: Send
 


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%.
+Pie Number of slices to send: Send
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();
}
}
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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

 
+Pie Number of slices to send: Send
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.
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1069 times.
Similar Threads
Constructors
Constructor Question
instance methods
whats this mean
Constructors
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 09:50:59.