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

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

Moderator: admin

Post Reply
fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

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

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

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

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

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

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

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

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

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

kevin35
Posts: 8
Joined: Mon Dec 25, 2017 4:30 am
Contact:

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

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

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

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

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

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

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

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

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

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

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

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

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

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

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

Post Reply

Who is online

Users browsing this forum: jjoseorione and 22 guests