Page 1 of 1

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

Posted: Wed Mar 05, 2014 11:46 pm
by fasty23
The explanation said:
"As static fields can be accessed even without creating an instance of the class, it is entirely
possible that this field can be accessed before even a single instance is created. In
this case, no constructor or non-static initializer had ever been called."

As B. Add the following line just after //2 : static { MAX = 111; CLASS_GUID =
"XYZ123"; } is correct, which means static initializer does not need creating an instance of the class.
Did I get right?

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

Posted: Thu Mar 06, 2014 12:03 am
by admin
Yes, that is correct.

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

Posted: Wed Aug 13, 2014 1:38 pm
by vchhang
Would you explain below?
Add the following line just before //1 : { MAX = 111; CLASS_GUID = "XYZ123"; }
This is not a static initializer and so will not be executed until an instance is created.
Option#3 is an instance initializer, correct? If so, wouldn't the static variable be initialized before the instance is created?

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

Posted: Wed Aug 13, 2014 7:18 pm
by admin
Yes, static variables will be initialized before an instance is created. That is why this option is wrong. It is possible to use MAX and CLASS_GUID before an instance is created and in that case, the values that you are giving to them in this instance initializer block will be of no use because the block will not even execute.

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

Posted: Sat Dec 27, 2014 12:12 am
by coder007
Explanation for the second option:
Initializing the static variables in a static block ensures that they are initialized even when no instance of the class is created.
It makes think that one-line initialization of static variable is not executed if an object of a class is not created. But it is. Static block and one-line initialization are equivalent ways to initialize static fields.

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

Posted: Mon Jan 22, 2018 6:40 am
by kevin35

Code: Select all

public class Test {
	static {
		x = 1;
	}
	static int x = 2;

	public static void main(String[] args) {
		System.out.println(x);
	}
}
OUTPUT: 2

Hi Paul, why this works? how can the static initializer modify the value of x, before its even declared?

I thought static blocks/variables are run in order of their apperance.
(thats why the output is 2)

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

Posted: Mon Jan 22, 2018 11:48 am
by admin
It works because not all forward references are restricted. One of the conditions of restricted forward references is that the use is not on the left hand side of an assignment.
In this case, the use is on left side of = operator. So it is ok.
You can take a look at section 8.3.3 of the JLS for the details.

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

Posted: Mon Jan 07, 2019 3:42 pm
by crazymind
Do these two cases compile?

class Widget{
static int MAX; //1
final String CLASS_GUID; // 2
{ MAX = 111; CLASS_GUID = "XYZ123"; }
Widget(){
//3
}
Widget(int k){
//4
}
}

OR

class Widget{
static int MAX; //1
static final String CLASS_GUID; // 2

Widget(){
MAX = 111; CLASS_GUID = "XYZ123";
}
Widget(int k){
//Do I need to initialize this in every constructors?
// MAX = 111; CLASS_GUID = "XYZ123";
}
}

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

Posted: Mon Jan 07, 2019 3:56 pm
by crazymind
Add the following line just before //1 : { MAX = 111; CLASS_GUID = "XYZ123"; }
This is not a static initializer and so will not be executed until an instance is created.
One more thing, what's do you mean by instance here? Do you mean an instance initializer or an object? Cause instance initializer get executed before constructor.

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

Posted: Mon Jan 07, 2019 9:59 pm
by admin
crazymind wrote:
Mon Jan 07, 2019 3:42 pm
Do these two cases compile?

class Widget{
static int MAX; //1
final String CLASS_GUID; // 2
{ MAX = 111; CLASS_GUID = "XYZ123"; }
Widget(){
//3
}
Widget(int k){
//4
}
}

OR

class Widget{
static int MAX; //1
static final String CLASS_GUID; // 2

Widget(){
MAX = 111; CLASS_GUID = "XYZ123";
}
Widget(int k){
//Do I need to initialize this in every constructors?
// MAX = 111; CLASS_GUID = "XYZ123";
}
}
What happened when you tried to compile these?

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

Posted: Mon Jan 07, 2019 10:00 pm
by admin
crazymind wrote:
Mon Jan 07, 2019 3:56 pm
Add the following line just before //1 : { MAX = 111; CLASS_GUID = "XYZ123"; }
This is not a static initializer and so will not be executed until an instance is created.
One more thing, what's do you mean by instance here? Do you mean an instance initializer or an object? Cause instance initializer get executed before constructor.
instance means an object of the class.