aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes same with super Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "same with super" Watch "same with super" New topic
Author

same with super

mo shaik
Greenhorn

Joined: Jul 17, 2007
Posts: 23
Asif Garhi
Greenhorn

Joined: Jul 24, 2007
Posts: 14
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?
Jared Folino
Greenhorn

Joined: Apr 27, 2007
Posts: 25
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.

nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
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");
}
}
Hari Krishna
Greenhorn

Joined: Nov 28, 2006
Posts: 12
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.


SCJP 93%<br />SCWCD 86%<br />SCBCD 92%<br />SCDJWS 95%
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
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 ]
 
Don't get me started about those stupid light bulbs.
 
subject: same with super