• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

Reg. class initialization

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some queries relating to class initialization :
1. When is a class exactly said to be initialized ?
2. Now when a class is initialization will all the members
variables whether static or non-static be initialized
to their default values ?
3. If the class extends another class then what is the sequence of
initialization for both super class and subclass ?
I am listing down what i have understood. Kindly correct if
anything wrong.
1. The "static" contants, variables, blocks of the SUPER class
will be executed.
2. The "static" constants variables, blocks of the SUB class
will be executed.
3. The "non-static" constants, varaibles, blocks of the SUB class
will be executed.
4. Followed by execution of the SUPER class constructor.
5. The "non-static" constants, variables, blocks of the SUB class
will be executed.
6. Finally the constructor of the SUB class is executed.
 
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Angela,
I think u r right when an object of a subclass is being
instantiated. But when u r declaring an object refrence/ reference or reference variable of a class, say the subclass then only the class specific infos r loaded and not the constructors or non-static variables, constants, non-static initializers. Correct me if i m wrong....

------------------
Muhammad Ashikuzzaman (Fahim)
When you learn something, learn it by heart!
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So when is the class actually initialized.
When the main method is called or when the constructor
is called ?
 
Ashik Uzzaman
Ranch Hand
Posts: 2378
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela,
IMO, the necessary classes to run the program are loaded by JVM when u run the application (say by : "java MyClass" in command prompt)! During class initialization comes the question of statics and remeber main() is also static....

------------------
Muhammad Ashikuzzaman (Fahim)
When you learn something, learn it by heart!
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the mean when main is called the static variables , blocks
and constants are initialized.
And when i create an instance of the class with "new"
then the non-static variables, blocks and constants
are initialized .
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anjela
Static members, blocks & constants are initialized when the class is loaded.Non-Static are initialized during the creation of instance.
But,if we come to the point of initialization of any class, the initialization of class must take place before an instance of the class can be created or a static method of the class can be invoked.A class's superclass is initialized before the class itself is initialized.
Class initialization means initializing the static member variables.Similarly initialization of interface means initialization of static variables declared in interface.

 
And then the flying monkeys attacked. My only defense was this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic