File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

same with super

 
mo shaik
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Asif Garhi
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Jared Folino
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
nitin pokhriyal
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • 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");
}
}
 
Hari Krishna
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
dhwani mathur
Ranch Hand
Posts: 621
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic