aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Block execution 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 "Block execution" Watch "Block execution" New topic
Author

Block execution

nitude gupta
Ranch Hand

Joined: Sep 04, 2008
Posts: 34
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

Joined: Aug 29, 2008
Posts: 580



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.


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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.


SCJP 6
Rajshekhar Paul
Ranch Hand

Joined: Oct 17, 2006
Posts: 140
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.


When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
nitude gupta
Ranch Hand

Joined: Sep 04, 2008
Posts: 34
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

Joined: Feb 01, 2007
Posts: 18


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 .


SCJP5 85%
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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

Joined: Aug 03, 2008
Posts: 9305
    
  17

Please QuoteYourSources. Otherwise no one is going to help you...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rajshekhar Paul
Ranch Hand

Joined: Oct 17, 2006
Posts: 140
Exacly.. Agreed to Punit.
nitude gupta
Ranch Hand

Joined: Sep 04, 2008
Posts: 34
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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

Joined: Aug 03, 2008
Posts: 9305
    
  17

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

Joined: Sep 07, 2008
Posts: 265
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
 
Don't get me started about those stupid light bulbs.
 
subject: Block execution