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 :
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?
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 )
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.
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" ?
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.
... 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
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.
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.