• 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

from the scratch object creation

 
Ranch Hand
Posts: 2379
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 Pals,
Help me clearing the concept stated below.
The Concept:
-----------
Class is loaded in the RAM first. Now before the object is being created in the static main method, our JVM interpretes the static inititalizer code block or expressions first according to serial of decalrations in that class and then static variables also according to serial of decalrations . Now the constructor of the object in the main method is being executed after the instance initializer and expressions are executed.But if it has a super class then it goes to the super class static and non-statics according to same serial stated above and then super class constructor. Then after coming to the current class constructor it creates the object herealso after executing non-static initializer code blocks according to serial of decalrations.
Please correct and explain the concept stated above.

------------------
azaman
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashik,
You're fairly close
When a class is loaded, memory is allocated for the static fields and they are assigned their default values; except for 'finals' declared with a literal, these are never set to their default value.
Static fields and initializers are executed (according to the order they appear in the source code).
Once the class is properly loaded, an instance may be created. Memory is assigned for all the instance variables and they are set to their default values; except for 'finals' declared with a literal, these are assigned the literal and are never set to their default values.
The parameters for the ctor are evaluated.
Instance variables and initializers are executed, according to the order they appear in the code.
The remainder of the ctor is executed.
The process is recursive in that all the superclasses have to be created first.
The whole process is to ensure that every object will be in a 'consistent' state. For example, if class B extends class A which extends Object than the fields and methods class B inherits from class A and Object, as well as it's own fields and methods should all be available and accessible in any class B object once it is created.
See JLS §:12.4 & 12.5.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashik ,
Try this
<code size=15>
class Star{
static {
System.out.println("Static");
}
Star(){
System.out.println("Const");//this won't print , 'cause no object of Star exist
}
public static void main(String args[]){
System.out.println("main");
}
};
</code>
mess around with this , you'll see the flow .
[This message has been edited by Ashish Hareet (edited August 04, 2001).]
 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thnx Jane, Excellent explanation! Also to Asheesh.
Regarding final variables, if we do not initialize static or non-static finals in the same line of variable declaration or in initializer block or expresion or in constructor it gives a compile time error. Right?
Code :


------------------
azaman
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jane,
An interesting thing to note is that apparently the class loading step happens before the JVM even executes the main() method of the class.
Check the following code:

It compiles successfully and when I run it, it prints "X static initializer" before complaining that it has no "main" method.
just my 2 cents..
- eric
 
Ashik Uzzaman
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excellent discovery Eric!

------------------
azaman
 
reply
    Bookmark Topic Watch Topic
  • New Topic