About Question enthuware.ocajp.i.v7.2.1158 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Daniel Clinton
Posts: 29
Joined: Fri Aug 08, 2014 11:22 am
Contact:

About Question enthuware.ocajp.i.v7.2.1158 :

Post 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:

Code: Select all

 s1 = new InitTest().sM1("b");
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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post 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.
If you like our products and services, please help us by posting your review here.

coder007
Posts: 25
Joined: Wed Dec 17, 2014 9:29 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post 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

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post 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
Last edited by admin on Thu Jan 29, 2015 3:42 am, edited 1 time in total.
Reason: Applied code tags

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post 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.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by gparLondon »

Thanks for your reply, I got my answer after slight modification to my original code.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post 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.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post 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

sir_Anduin@yahoo.de
Posts: 62
Joined: Fri Aug 07, 2015 2:16 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post 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).

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by admin »

Java does allow forward referencing in this situation.
If you like our products and services, please help us by posting your review here.

vlezz94
Posts: 12
Joined: Wed Sep 28, 2016 6:31 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post 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;
    }
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by admin »

Lets look at it from a different angle - Which line do you think should be called after s1 = sM1("1");?
-Paul.
If you like our products and services, please help us by posting your review here.

AndaRO
Posts: 31
Joined: Wed Feb 08, 2017 5:42 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by AndaRO »

Because the constructor is the last who is executed.

acharnamys
Posts: 2
Joined: Mon Nov 20, 2017 8:00 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post 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".

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by admin »

Are you sure? I just ran the code and it printed a b c 2 3 4 1

Paul.
If you like our products and services, please help us by posting your review here.

acharnamys
Posts: 2
Joined: Mon Nov 20, 2017 8:00 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1158 :

Post by acharnamys »

Right, got confused as Intellij formatted the code in different order. Thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 35 guests