This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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
posted
0
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
posted
0
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.
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.
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
posted
0
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
posted
0
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 ]
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 ]
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.
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
posted
0
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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.