About Question enthuware.ocajp.i.v7.2.1243 :
Moderator: admin
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1243 :
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?
"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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1243 :
Yes, that is correct.
If you like our products and services, please help us by posting your review here.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1243 :
Would you explain below?
Option#3 is an instance initializer, correct? If so, wouldn't the static variable be initialized before the instance is created?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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1243 :
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.
-
- Posts: 25
- Joined: Wed Dec 17, 2014 9:29 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1243 :
Explanation for the second option:
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.Initializing the static variables in a static block ensures that they are initialized even when no instance of the class is created.
-
- Posts: 8
- Joined: Mon Dec 25, 2017 4:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1243 :
Code: Select all
public class Test {
static {
x = 1;
}
static int x = 2;
public static void main(String[] args) {
System.out.println(x);
}
}
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)
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1243 :
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.
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.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1243 :
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";
}
}
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";
}
}
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1243 :
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.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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1243 :
What happened when you tried to compile these?crazymind wrote: ↑Mon Jan 07, 2019 3:42 pmDo 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";
}
}
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1243 :
instance means an object of the class.crazymind wrote: ↑Mon Jan 07, 2019 3:56 pmOne 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.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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 73 guests