• 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

About instance in. and static initializer

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class MyClass
{
public static void main(String[] args)
MyClass obj = new MyClass(l);
static int i=5;
static int l;
int j=7;
int k;
public MyClass(int m )
{
S.o.p(i + ", " + j + ", " + k + " ," + l + ", " + m );
}
{ j=70; l=20; }
static { i=50;}
}
Ans : 50,70,0,20,0
Above is an example from Khalid Mughal.
I got the answer as per the explanation given.
But i having some doubts
---------------------------------------------------------------
Below is what i have understood :
1. When the class is first initialized the static members are initialized
and static initializer blocks if any are executed.
2. The instance members at this same time to their default values.
3. Now when the instance of the class is created , the instance
variables are initialized and instance initializers are executed.
--------------------------------------------------------------
Below are my doubts :
Does this mean that static initializer blocks are executed before
the instance initializer blocks ?
Secondly is the execution of instance initializer blocks and variables
done just before execution of the constructor of the class ?
---------------------------------------------------------------
Can someone suggest me some good example which has a combination
of both instance initializer and static initializer ?
Thanks in advance !!
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, your understanding is correct.
The sequence of initialization is -
1. static initializers
2. instance initializers
3. constructors
As static code belong to the class, and not its instances, static initialization is done at the begining, when the class is loaded the first time, and only once.
When an instance is created, the instance intialization is done, followed by the constructors.
If you run the folowing code, you may understand better.
This also illustrates the sequence in the case of inheritance.
class A
{
A()
{
System.out.println("In constructor SUPER___3");
}
static int a = geta();
static int geta()
{
System.out.println("in static method SUPER___1");
return 1;
}
{
System.out.println("in instance method SUPER___2");
}
}
class B extends A
{
B()
{
System.out.println("In constructor SUB___6");
}
static int a = geta();
static int geta()
{
System.out.println("in static method SUB___1/4");
return 1;
}
{
System.out.println("in instance method SUB___5");
}

public static void main(String args[])
{
new B();

}
};
 
reply
    Bookmark Topic Watch Topic
  • New Topic