Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Tue Jul 30, 2013 2:19 pm
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

Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Wed Jan 08, 2014 11:30 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Wed Oct 29, 2014 3:56 am
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

)
Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Sat Nov 14, 2015 4:35 pm
by Roibeard
Hello,
In what way does the constructor call "b"?
Or, maybe, where is the constructor?
Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Sat Nov 14, 2015 10:38 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Fri Nov 20, 2015 4:57 am
by Roibeard
Thank you that's helpful.
how is this line of code s1 = sM1("b"); called?
is what I was trying to ask!
Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Sun May 27, 2018 12:37 am
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!
Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Mon May 28, 2018 9:10 pm
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?
Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Tue Feb 19, 2019 9:30 pm
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
Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Tue Feb 19, 2019 9:43 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Tue Feb 19, 2019 10:10 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Tue Feb 19, 2019 10:23 pm
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.
Re: About Question enthuware.ocajp.i.v7.2.1228 :
Posted: Tue Feb 19, 2019 10:53 pm
by OlgaAG
I saw it.

Thank you!