Page 1 of 1
About Question enthuware.ocajp.i.v7.2.955 :
Posted: Sun Mar 24, 2013 9:57 am
by Travis Grey
Hi!
The question "An instance member ..." has 2 correct options:
1) can be a variable, a constant or a method.
2) belongs to an instance of the class.
But "constant" by definition should be "static" (and "final"), that is "constants are not instance member".
Please, help to understand.
Re: About Question enthuware.ocajp.i.v7.2.955 :
Posted: Sun Mar 24, 2013 10:43 am
by admin
Travis Grey wrote:
But "constant" by definition should be "static" (and "final")
That is not true. A constant is something that doesn't change. So final, yes. But static or instance is a completely unrelated thing.
Re: About Question enthuware.ocajp.i.v7.2.955 :
Posted: Thu Dec 19, 2013 5:03 am
by Metz123
"An instance member is a member variable or a member method that belongs to a specific object instance."
Does a member method really belong to an instance? If you have a (non-static) member function foo() and 2 instances i1 and i2, the member function foo() is absolutely equal for both instances, isn’t it? Or is it defined as different due to the hidden parameter “this”.
Re: About Question enthuware.ocajp.i.v7.2.955 :
Posted: Thu Dec 19, 2013 5:08 pm
by admin
That is right. Instance method operates on the particular instance of that class ("this").
-Paul.
Re: About Question enthuware.ocajp.i.v7.2.955 :
Posted: Mon Apr 20, 2015 3:42 pm
by Denn777
Travis Grey is right.
Constants are not instance members.
"Constants" chapter:
http://docs.oracle.com/javase/tutorial/ ... svars.html
Re: About Question enthuware.ocajp.i.v7.2.955 :
Posted: Mon Apr 20, 2015 8:23 pm
by admin
As per JLS Section 4.12.4:
A variable of primitive type or type String, that is final and initialized with a compile-time constant expression, is called a constant variable.
This implies that being static is not a requirement for a variable to be constant.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.955 :
Posted: Tue Jan 24, 2017 7:31 pm
by dvc1190
This question doesn't distinguish between a run-time constant and a compile-time constant. Per Oracle documentation, a Java constant is a compile time constant, so it must be static and final. A compile-time constant is also what most people in the field will mean when they use the term "constant".
If the test really will be vague about "constant", then this question can remain. Otherwise, please change this question as your justification is misleading.
Re: About Question enthuware.ocajp.i.v7.2.955 :
Posted: Tue Jan 24, 2017 11:09 pm
by admin
The answer and explanation are correct. Please go through the Java language specification quoted above.
A constant - irrespective of whether it is compile time or run time - doesn't have to be static.