• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

When instance methods become accessible during construction

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code from one of the practice exams which looked like this:
class G {
String s1 = "G.s1";
void printS1(){System.out.print("G.printS1," + s1);}
G() { printS1();}
}
class H extends G {
String s1 = "H.s1";
void printS1(){System.out.print("H.printS1," + s1);}
public static void main(String[] args) {
H h = new H();
}
}
had an answer that the result was to print H.printS1,null. It's the first part of this that I don't understand. printS1() is called within the constructor of the superclass. It is not a static method, so how can it be called using the subclass version when there isn't an instance of the subclass yet -- or at least I thought there wasn't. And that understanding seems consistent with the K and B book statement that "You cannot make a call to an instance method or access an instance variable until after the super constructor runs." But isn't it in the process of running when it makes this call? Thanks,
Kathy
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is tricky one.
I will put numbers with brief explanation. Follow the numbers to see the sequence.

1. H constructor called
2. compiler places default constructor for H class, which calls super constructor G().You can't see it because JVM places implicitly call.
3. G() constructor executes, calls printS1() method
4. prints s1 which is still uninitialized "null". Constructor at line 3 hasn't finished yet
Gee...It seems I can't indent the lines..

[ July 23, 2003: Message edited by: Alex Radomski ]
[ July 23, 2003: Message edited by: Alex Radomski ]

[ July 23, 2003: Message edited by: Alex Radomski ]
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kathy,
I'm sure this will help you, especially the explanations from Joel McNary.
https://coderanch.com/t/242443/java-programmer-SCJP/certification/inheritance
Cyril.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very informative link, Cyril and explains several other great issues to be aware of as well. Highly recommended.
BTW, I just finished K&B's chapter 6 in the cert guide (Strings, java.lang.Math, etc.) and while I got most of the questions correct, boy do I have to study those charts again....it was requiring WAY too much thinking time which showed that while I had the concepts down, it was just barely (ie., rules that apply to the Math class versus a Math instance in particular).
Java is so awesome though, and I really owe a big thanks to K & B for making it as such...there is no pair of books I'd recommend more highly, but for those of you having trouble understanding many aspects of the cert guide, it would be well worth your time to take a 2 or 3 week detour and read their new Head First Java book...THAT is what really cemented the foundation for me that is allowing me to learn everything else much more effectively.
Again, great thread you referred to Cyril (and it steps through the question that began this thread).
Ross
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kathy Hodgson:
... so how can it be called using the subclass version when there isn't an instance of the subclass yet ...
Kathy


Hi Kathy,
Not exactly true. There is already an instance of the subclass i.e. the VM had already allocated a memory for the new object. However, it hasn't gone thru the initialization phase yet at that point.
[ July 23, 2003: Message edited by: Alton Hernandez ]
 
Kathy Hodgson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all; these help, especially the link. However, even that says that this.prints1() is called WHILE the superclass constructor is executing. How can that be OK, when it is an instance method, and the K&B book says it's not allowed to call an instance method until the superclass constructor is finished executing? Am I taking that too literally? If so, can anyone give an example of when calling an instance method before the superclass constructor is finished executing actually would cause a problem? I still don't understand when it's OK and when it's not.
Kathy
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kathy Hodgson:
Thank you all; these help, especially the link. However, even that says that this.prints1() is called WHILE the superclass constructor is executing. How can that be OK, when it is an instance method, and the K&B book says it's not allowed to call an instance method until the superclass constructor is finished executing? Am I taking that too literally? If so, can anyone give an example of when calling an instance method before the superclass constructor is finished executing actually would cause a problem? I still don't understand when it's OK and when it's not.
Kathy


I am just guessing here because I don't have a copy of that book.
Perhaps what they are talking about has something to do with calling a method before super(). That action will cause compilation to fail, just like the code below:

The original code does not violate this rule because there is an implicit call to super() before printS1(). So the superclass of G, which is the Object class, had already been initialized before printS1() is called.
 
Kathy Hodgson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Alton, that makes sense!
Kathy
reply
    Bookmark Topic Watch Topic
  • New Topic