• 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

Classes Initialization order

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have a class that has the folowing members:
1- static variabls.
2- static block.
3- public variabls.
4- static methods
5- private variabls.
6- public method
and a public constructor.
when I instantiate the class from somewhere invoking "new"
what is the order of intializing the previous members??
thank you
 
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 Hanna Habashy:
If I have a class that has the folowing members:
1- static variabls.
2- static block.
3- public variabls.
4- static methods
5- private variabls.
6- public method
and a public constructor.
when I instantiate the class from somewhere invoking "new"
what is the order of intializing the previous members??
thank you


When you compile a java program, the compiler generates an two initilization methods, namely <clinit> and <init>. The <clinit> method is the class initialization method that is invoked whenever a class is first loaded. The <init> method is the instance initialization method that is invoked when an instance is created.
The <clinit> method consists of static variable initializers and the codes in the static initializers. The order will depend in the order they are declared in the source codes.
The <init> method consists of (in the following order):
1. call to the superclass <init> method.
2. instance variable initializers(regardless of their access modifiers) and instance initializers; again the order depends on the order they are declared in the source
3. the rest of the codes of the constructor.
Hope this helps
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I short,
1. virtual machine load the class
2. the class then is verified
3. after verification the class is initialized (execute initalizer)
4. initialized class variable (static variable)and static initializer (depends on the textual order)
5. when "new" is called the class constructor is called
6. all super classes constructor is called and initialized all their instance variables
7. the rest of the class constructor code is the executed.
 
Richard Teston
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In short...
1. virtual machine load the class
2. the class then is verified
3. after verification the class is initialized (execute initalizer)
4. initialized class variable (static variable)and static initializer (depends on the textual order)
5. when "new" is called the class constructor is called
6. all super classes constructor is called and initialized all their instance variables
7. the rest of the class constructor code is the executed.
 
I can't take it! You are too smart for me! Here is the tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic