Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Fri Oct 03, 2014 12:42 pm
by Daniel Clinton
Tough question to open with!
Here's something I've noticed and would like to ask about.
If the assignment of the string returned from method sM1 to static field s1
in the static init block is accomplished via an object reference:
the output is: a 2 3 4 1 b c 2 3 4 1
Does this mean the this first object loads within the loading of the class?
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Fri Oct 03, 2014 8:17 pm
by admin
Yours is not an easy question to answer, you will need to go through
section 12.2, 12.3, 12.4, and 12.5 of JLS. Specially, section 12.4.2.
But note that you are going deeper than required for the exam.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Fri Dec 26, 2014 1:02 am
by coder007
To complete I'd like to add that:
First, all the fields are initialized to their default values (0, null, false)
and than:
static statements/blocks are called IN THE ORDER they are defined.
Next, instance initializer statements/blocks are called IN THE ORDER they are defined.
Finally, the constructor is called
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Thu Jan 29, 2015 2:33 am
by gparLondon
I got this question right, only because I knew the behaviour of static method, Initializers and variables. I thought I know about the behaviour of the instance variables and initializer block and constructor too but I am bit stuck here. Can you please help?
I have this code,
Code: Select all
public class B extends A{
static A a=new A(1);
A aa=new A(3);
public static void main(String args[])
{
new B();
}
static A b=new A(2);
}
public class A {
A()
{
System.out.println("A");
}
A(int i)
{
System.out.println(i);
}
}
The Out put of this program is 1 2 A 3.
Till going through this question in enthuware I thought, Instance initializer block runs first, then constructor finishes, then initialization of instance variables finishes. According to which I thought the output of this program will be a b c 3 1 2 4. but the exact match was not found in the option so went for nearest one. Can you please explain me the behaviour of these 2 programs.
Thanks in advance,
GPAR
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Thu Jan 29, 2015 3:04 am
by admin
Please go through the JLS sections mentioned above and then go through your program. If you still have any doubt, let me know.
-Paul.
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Thu Jan 29, 2015 3:24 am
by gparLondon
Thanks for your reply, I got my answer after slight modification to my original code.
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Thu Jan 29, 2015 3:42 am
by admin
Great! Please use code tags while posting code here so that it is easier to understand.
If you can, please post your findings as they will be helpful to others.
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Thu Jan 29, 2015 7:29 am
by gparLondon
In my last post the confusion was, I was calling Super class constructor, and it was finishing first then my base class constructor, I will put this in a summary,
1>In a class static code(initializer, block or variable) get executed only once for that class
2>The point no.1 happens whenever the class is loaded
3>It will be executed in the order of its description
4>The class is loaded whenever, a static variable is initialized or static method is called or object of that class is created.
5>Whenever the object of the class is created the constructor is called, in the order up in the hierarchy example A<-B<-C. upon object creation of class C it will call default constructor (assuming no other constructors exists in A,B,C) of B which inturn calls default constructor of A.
6>Now all the default initializer blocks and instance variable initialization gets completed for A in the order of existence. Constructor A finishes control flows back to B, now Constructor B follows the same step and finishes. Now its the turn of Constructor C it also follows the same steps.
7>Step no. 5 and 6 will be repeated every time when object for class c gets created.
The confusion for me was I saw the constructor A finishing first and then, initialization of instance variable in B happening, I got mixed up the 2. I fixed it by adding following code to B.
Code: Select all
B()
{
System.out.println("Boo");
}
To Admin, Sorry in my previous message I accidently added Quote instead of Code, the 2 tabs are next to each other.
Hope this explanation doesn't confuse anyone.
Regards,
GPAR
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Tue Aug 11, 2015 5:40 pm
by sir_Anduin@yahoo.de
How is it possible for s1 to be assigned the value "2" if it was not defined yet:
Code: Select all
public class Main
{
static{ s1 ="2"; }
static String s1 = "1";
public static void main(String args[])
{
Main m = new Main();
}
}
1. Static blocks and static statements of the base class (only once, in the order they appear in the class).
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Tue Aug 11, 2015 7:46 pm
by admin
Java does allow forward referencing in this situation.
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Thu Nov 17, 2016 7:42 am
by vlezz94
Hi!
right now I'm kind of confused with this code. I understand that the static statements/blocks are called first in the order they are created. But I don't see why "1" it's the last one called:
Code: Select all
public class InitTest{
public InitTest(){
s1 = sM1("1");
}
static String s1 = sM1("a");
String s3 = sM1("2");{
s1 = sM1("3");
}
static{
s1 = sM1("b");
}
static String s2 = sM1("c");
String s4 = sM1("4");
public static void main(String args[]){
InitTest it = new InitTest();
}
private static String sM1(String s){
System.out.println(s); return s;
}
}
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Thu Nov 17, 2016 10:25 pm
by admin
Lets look at it from a different angle - Which line do you think should be called after s1 = sM1("1");?
-Paul.
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Fri Feb 17, 2017 12:30 pm
by AndaRO
Because the constructor is the last who is executed.
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Mon Nov 20, 2017 8:06 am
by acharnamys
At the moment with the following question there is no correct answer.
Code: Select all
public class InitTest{
public InitTest(){
s1 = sM1("1");
}
static String s1 = sM1("a");
String s3 = sM1("2");{
s1 = sM1("3");
}
static{
s1 = sM1("b");
}
static String s2 = sM1("c");
String s4 = sM1("4");
public static void main(String args[]){
InitTest it = new InitTest();
}
private static String sM1(String s){
System.out.println(s); return s;
}
}
Possible answers:
- The program will not compile.
- It will print : a b c 2 3 4 1
- It will print : 2 3 4 1 a b c
- It will print : 1 a 2 3 b c 4
- It will print : 1 a b c 2 3 4
The closest answer is "a b c 2 3 4 1" which is still far from the reality "a c b 2 4 3 1".
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Mon Nov 20, 2017 11:21 pm
by admin
Are you sure? I just ran the code and it printed a b c 2 3 4 1
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1158 :
Posted: Tue Nov 21, 2017 12:06 am
by acharnamys
Right, got confused as Intellij formatted the code in different order. Thanks