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

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

Moderator: admin

Post Reply
CreepyMulder
Posts: 4
Joined: Sat Jul 27, 2013 3:45 pm
Contact:

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

Post by CreepyMulder »

Hello,

I got this question right, and I understand the order in which each statement are executed. I've made some modification and I don't understand the following :

Code: Select all

public class InitTest{
	
   static
   {
	  // System.out.println(s1); < Does not compile because S1 hasn't been initialized
	   s1 = sM1("c");
	  // System.out.println(s1); < Does not compile because S1 hasn't been initialized
   }
	   
   static String s1 = sM1("a");
   
   {
      s1 = sM1("b");
   }

   public static void main(String args[]){
      InitTest it = new InitTest();
   }
   private static String sM1(String s){
      System.out.println(s);  return s;
   }
}
The output is "CAB", but how can "C" be printed when the variable S1 is only initialized when "A" is printed?

Thanks ;)

JeramieH
Posts: 22
Joined: Wed Jan 08, 2014 11:24 am
Contact:

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

Post by JeramieH »

Tried your code myself and you're right. It appears the sM1("c") code executes, but the return value is discarded since s1 doesn't exist yet.

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

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

Post by Daniel Clinton »

Hi Paul,
Possibly a small error:
The explanation says "Finally, the constructor is called. So, then it prints b"
Isn't b printed by the method called in the instance initializer?

(Also, is there any possibility of legal vs. illegal forward reference appearing in this exam
- like in the above code from CreepyMulder?
I read the JLS on this topic once before and decided to leave it when my head hurt :) )

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post by Roibeard »

Hello,

In what way does the constructor call "b"?
Or, maybe, where is the constructor?

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

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

Post by admin »

I am not sure understand your question. Whose constructor are you looking for or expecting?

The main method contains InitTest it = new InitTest(); This line invokes the default constructor of class InitTest. This class does not provide any constructor expliclty, so the compiler automatically adds a default constructor to this class. As is evident from the above line of code, the default constructor provided by the compiler does not take any argument. You may want to read more about default constructor here: http://stackoverflow.com/questions/4488 ... onstructor

I don't understand what you mean by "what way does the constructor call "b"?". If you mean how is this line of code s1 = sM1("b"); called? Then you should observe that this line in a part of the instance initializer block of class InitTest. The code in this block is executed before the code in the constructor. You should read about static and instance initializer blocks to learn about this.

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

Roibeard
Posts: 19
Joined: Sun Jul 12, 2015 6:40 am
Contact:

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

Post by Roibeard »

Thank you that's helpful.
how is this line of code s1 = sM1("b"); called?
is what I was trying to ask!

mihhay
Posts: 10
Joined: Wed May 09, 2018 12:48 pm
Contact:

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

Post by mihhay »

Hi Paul,
1)Reading about default constructor, from your first link , made me more confused. In the first response (that one with 119 votes) they are debating if default constructor, " It initialises any uninitialised fields to their default values " or not... there are pro and cons arguments....I do not know what to believe anymore...
So what is the correct answer? it does initialize or not?

2) Reading from your second link, I am trying to figure out what/why/how is called : { s1=sM1("b");
Is this { etc } an "initializer block" ?

Thank you!

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

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

Post by admin »

1. Technically,the default constructor provided by the compiler doesn't initialze anything. Whether it should or not, depends on the business logic of the class.

2. It is an instance initializer block. Which book are you following?
If you like our products and services, please help us by posting your review here.

OlgaAG
Posts: 10
Joined: Thu Jan 03, 2019 3:08 pm
Contact:

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

Post by OlgaAG »

admin wrote:
Sat Nov 14, 2015 10:38 pm
... Then you should observe that this line in a part of the instance initializer block of class InitTest. ...
HTH,
Paul.
Hello!
Why does s1 = sM1("b"); is prints last and is in a part of the instance initializer block? s1 has a static modifier, which means that is a class variable. Statics are initialized first in the code. Hence, should be printed a, b and c

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

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

Post by admin »

The statement s1 = sM1("b"); is inside curly braces { } and there is no static modifier applied to this block. That is why it is a part of an instance initializer.
If you like our products and services, please help us by posting your review here.

OlgaAG
Posts: 10
Joined: Thu Jan 03, 2019 3:08 pm
Contact:

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

Post by OlgaAG »

But static String s1 = sM1("a"); is inside the same curly braces { } too. What is the difference? The variable is the same, the block is the same.

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

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

Post by admin »

OlgaAG wrote:
Tue Feb 19, 2019 10:10 pm
But static String s1 = sM1("a"); is inside the same curly braces { } too.
No, it is not. Read the code carefully.
static keyword is not allowed inside a code block. The code wouldn't compile if static String s1 = sM1("a"); is inside curly braces.
If you like our products and services, please help us by posting your review here.

OlgaAG
Posts: 10
Joined: Thu Jan 03, 2019 3:08 pm
Contact:

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

Post by OlgaAG »

I saw it. :lol: Thank you!

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests