• 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

confusion in variable initialization concepts

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All!


The out put of the main() method is "value of a is 0". Please explain the flow. It is a bit confusing for me.

Thanks
Ansar Shah
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In main method, you are creating a instance of Trick(), so Trick constructor will get called and consequntly Super class(A) constructor will get called. In Super class constructor, you are calling Show() method.
Here int a =8 and int a =90 are instance variable and instance variable runs after the compiler's call to super.
So when show() method get called , value of a was 0(default value).
So it is displaying 0.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Ansar,

thanks for this great example. It was a real eye opener for me. The call to show() in the super class A will always point to the implementation in the Subclass Trick. At wich point the variable a of class Trick still has default value 0.
I would never have expected that. This is even the case when class A is not abstract and has it's own implementation of show().
In other words, the next piece of code gives exactly the same result.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you everybody for raising this eye opening question. After a long time
seeing this kind of question.

Already you ranchers have given your fantastic examples:
See little modified code as well:



It prints "value of a is " + 8
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This topic is well-covered in "Thinking in Java" by Bruce Eckel.
Polymorphic and constructors.



output is


I try to summarize what I remember:

1. class loader must load the class (this can happen when you want to create new object or access some static mameber.
2. if this class has base class, base is load prior to descendant (of course)
3. class objects are created (from the top (base) to bottom descendant), all static members are initialized (from top to bottom)
4. when there is all information needed how new object will look like, the memory is allocated and set to binary zero
5. constructor of class is called (which calls base constructor first)
6. when base class is constructed all non-static members are initialized
7. body of constructor is executed

(If I screwed up something, please correct me)
reply
    Bookmark Topic Watch Topic
  • New Topic