• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

this

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Just testing you.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you have a question about this, Marlene? Or did you just think it was an interesting question?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No question. Thanks for checking.
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where ever you look, it's nothing but B!
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry but I'm kinda confused. I thought it should print A and then B. First the constructor of super class is executed before the consturctor of the child class. Correct?
Thanks,
deep
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sorry but I'm kinda confused.

Ah, the person I am looking for.

First the constructor of super class is executed before the consturctor of the child class. Correct?

Memory for the B object is allocated. Then the constructor B() is invoked. The first thing B() does is invoke A().
When the compiler is compiling the constructor A() for class A, how does it know what to assign to the variable a?
I think, the constructor B() invokes A() and passes a reference to the B object under construction to the constructor A().
Or put another way, B() invokes A() and leaves a reference to the B object under construction hanging around on the stack for the constructor A() to use.
[ April 10, 2003: Message edited by: Marlene Miller ]
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak,
What you said is correct.
But you are suppose to get this output as:
Class B
Class B
bcoz, when baseclass constructor is called it refers to the instance of child class(B), so while in SOP a.getClass() object prints Class B, it mean this refers to object B's Instance.
This always refers to current instance, here the current instance is B. (A a = new B());
Correct me if I am wrong.
Regards
PREM
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this mean that in A(), you could cast this to B and access, for example, an uninitialized instance variable of B?

so foo would be false because it was not yet initialized?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bryan Helm:
Does this mean that in A(), you could cast this to B and access, for example, an uninitialized instance variable of B?


Indeed it does. this is a reference to the current object. As we haven't yet initialized the components of that object defined in the subclass, any of those members won't be initialized. Try this:

We end up with false because, when we retrieve foo, it hasn't yet been initialized.
Corey
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Brian, you are right. This is also the reason why constructors should not make polymorphic calls: the overriding method could be accessing variables not initialized yet.
___________________________________________________
The declared (compile) type of this is the class containing the method in which this appears.
this, within an instance method points the instance on which the method was invoked.
this, within a constructor points the instance that is being constructed. In this way the parent constructor initializations are performed over the instance being created, as it should be
this is called the hidden argument because you do not see it in the argument list, but it appears magically as the first local variable within the method or constructor.
the hidden argument to a constructor is the reference to the object returned by new (just been allocated space)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic