• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

same with super

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dude keep this dream sequence in mind when you do a new()

1.Instance variables are given their Default values.
2.Super constructor is called.
3.Other instance variables that are given initial value as a part of declaration are assigned values or init block is run --- *whichever appears first in the code*.
4.Rest of the constructor finishes.

Try to do these while solving your query you will get the answer.

Also remember that static initializer if there in your code are the first thing to happen before any of the above activities.

Right Ranchers?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Intialisation Blocks and instance variables are ALWAYS run before the constructor. But what you need to understand is the order in which they execute. This is one of the few places were Java actually works in a (slightly) structured way. It executes initialition blocks and instance variables in the order they appear in the class.

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
apart from that static block is invoked at the time of class loading.. run example to get the flow.

package another;

class Parent{ int a = getA();
{
System.out.println("Parent Class Instance Initializer");
}
//here loaded first
static{
System.out.println("initalizer block of parent");
}

public Parent()
{
System.out.println("Parent Class Constructor");
}
private int getA() {
System.out.println("Parent Class Member Initializer");
return 0;
}
}

public class Child extends Parent{

//here loaded second
static{
System.out.println("initalizer block of child");
}
{
System.out.println("Child Class Instance Initializer");
}
int b = getB();
public Child() {
System.out.println("Child Class Constructor");
}

private int getB() {
System.out.println("Child Class Member Initializer");
return 0;
}

public static void main(String[] args) {
Child c = new Child();
System.out.println("Done");
}
}
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A small add-on info for the above post..

The Sequence will be like this if we run the above program

Parent Class Static Initializer
Child Class Static Initializer
Parent Class Member Initializer -->1
Parent Class Instance Initializer -->2
Parent Class Constructor
Child Class Instance Initializer -->3
Child Class Member Initializer -->4
Child Class Constructor

But there is no sequence for Member Initializer and Instance Initializer.
They have equal preference therefore JVM executes whichever comes first.

Thank you,
Hari Krishna.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the concept of initialisation getting executed first but i am having
a doubt which will sound quite simple to you all,it is shown in the code below i have highlighted..........



First of all value of variable a gets initialised,but later,the block...
Although static is not mentioned in the block so what this block of code is?
does this block also initialises value of a?
please can anyone explain............
[ July 26, 2007: Message edited by: dhwani mathur ]
reply
    Bookmark Topic Watch Topic
  • New Topic