• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Block execution

 
nitude gupta
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have come across this question in practise for SCJP:

class Bird {
{ System.out.print("Bird1 "); }
public Bird() { System.out.print("Bird2 "); }
}
class Raptor extends Bird {
static { System.out.print("Raptor1 "); }
public Raptor() { System.out.print("Raptor2 "); }
{ System.out.print("Raptor3 "); }
static { System.out.print("Raptor4 "); }
}
class Test extends Raptor {
public static void main(String[] args) {
System.out.print("pre ");
new Test();
System.out.println("Test");
}
}

OUTPUT is
Raptor1 Raptor4 pre Bird1 Bird2 Raptor3 Raptor2 Test

I just want to know what is the rule for execution of the blocks which are not marked static and get executed when the object is created???


It may be very beginer level but it will help me to brush & buid my concepts......
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Those are know as initilzation block.Which gets executed before any statements of that particular class's constructor are executed.
(its like they are moved in to the begining part of every constructor of its class. )

I guess now you would get the answer.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Block execution happens after execution of super class constructor.

lets take example that will clear you.



But compiler will make change and will do something like this.



I have outlined the order of execution.

new B() will call constuctor of B.
constructor B() will call constructor of A;
constructor A() will call constructor of Object;
After executing constructor of Object, control comes back to class A constructor.
Then block {System.out.println("Block A");} will be executed.
then code in constructor System.out.println("Cons A"); will be executed.
Same way for constructor B();

Means Block code is executed just after call to super constructor.
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add to Punit's reply -
The execution of the initializer blocks no matter static or instance will be done according to their textual order in the source file and the initializer blocks do not permit forward reference.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nitude gupta:
I have come across this question in practise for SCJP: ...


We have a rule here: When you post a question copied from a book, mock exam or other source, we require you to quote your sources. So, please tell us where you copied the question from.
 
nitude gupta
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much.....
for the detailed explanation along with example....
That was really helpful Punit

Rajshakher thank you for extra bit information, just want to reconfirm that by forward refrences you mean somthing like
int i=j= 0;
int K=j; is nt permitted or something else please correct!! if I am totally on wrong track Jasper originally the question
[ December 17, 2008: Message edited by: nitude gupta ]
 
Jayati Das
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In the above code first all the static blocks will print in the order of appearance .So Raptor1 Raptor4 get printed
Then the execution would come to public static void main(String[] args) and pre gets printed
then the new Test() pases the execution to its super classes constructor
public Raptor()
which calls its super class Birds constructor 'public Bird()'.
Now the execution passes to the code in Bird class so Bird1 gets printed .
After this it comes back to the Bird classes constructor and so Bird2 prints.
After finishing Bird class constuctor the control passes back to its subclass constructor public Raptor().Before executing tis contructor it has to execute rest of the code in Raptor class so Raptor3 gets printed .
then the RAptor constructor code runs and Raptor 2 prints .
Now the control passes to Raptors subclass Test ans Test gets printed .
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A{
int i=j;
int j=5;
}



This is called forward reference,
int i=j; here you are using j before declaring it.

static int i1=j1;
static int j1=0;


In static case also forward reference not permitted.

while here in constructor it is possible.
class A{
public A(){int i2=j2;
int k2=l2;
}
static int j2;
int l2;
}


As constructor is executed after static variable and variable initialization.


{int m3=j3;}

static int j3;


as static int j3; is executed before {int m3=j3;}

{int m4=j4;//error
}
int j4;



int m4=j4; not permitted as initializer block is executed after variable initialization.

[ December 17, 2008: Message edited by: punit.singh ]
[ December 17, 2008: Message edited by: punit singh ]
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources. Otherwise no one is going to help you...
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exacly.. Agreed to Punit.
 
nitude gupta
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Punit once again!!! I think there is mixing with variable names in your example If I am nt wrong.....I am able to make out what you mean to say but if you edit it would be more clear

Ankit ,the question was from some old notes,yrs back when I tried to learn java and left hopes!!!
If source is notes then also we need to mention???
or what do we do in that case...??
[ December 17, 2008: Message edited by: nitude gupta ]
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nitude gupta:
If source is notes then also we need to mention???
or what do we do in that case...??

Yes, we always want you to quote your sources, also if it's from your own notes. Please look at the quote your sources page, it explains why this is necessary.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nitude quoting the source is just an effort to stop people from posting illegal questions. If you have created the question yourself, then also you must mention that this program is a test program created by me or something like that. This helps in understanding that you have created the program yourself and not copied it from anywhere...
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

i think it's a bad idea to do the declaration and initialization part in constructor or any initializaton block. if you do this you cannot invoke the variable, you will get an error as " variable cannot find symbol".

please correct me if i am wrong.

Preetha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic