Page 1 of 1
About Question com.enthuware.ets.scjp.v6.2.189 :
Posted: Mon Mar 19, 2018 3:01 pm
by TwistedLizard
Option 2:
Non-static inner classes cannot contain static members.
Commentary:
They can if the static variable is also made final.
Is this only for primitive member variables? Attempting to declare a final reference variable in a non-static inner class results in a compiler error, in this case at least:
Code: Select all
import java.util.Date;
class Test{
class Inner{
final static Date date = new Date(); //illegal static declaration
}
public static void main(String[] args){}
}
Re: About Question com.enthuware.ets.scjp.v6.2.189 :
Posted: Mon Mar 19, 2018 10:47 pm
by admin
JLS Section 8.1.3 clearly states:
It is a compile-time error if an inner class declares a member that is explicitly or
implicitly static , unless the member is a constant variable (§4.12.4).
And as per 4.12.4,
A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.28)
So for example, static final String s = ""; compiles but static final String s = new String(); doesn't.
That's explains why your code fails to compile.
Re: About Question com.enthuware.ets.scjp.v6.2.189 :
Posted: Wed Mar 21, 2018 3:21 am
by TwistedLizard
ok. Thanks for your help.
Re: About Question com.enthuware.ets.scjp.v6.2.189 :
Posted: Fri May 16, 2025 1:56 am
by nkaragulov
Since Java 16, Inner classes can declare non-final static members as well.
To create a class variable for an inner class, the programmer must place the desired variable in an enclosing class.
It is helpful at this point to abuse the terminology somewhat, and say, loosely, that the static keyword always marks a "top-level" construct (variable, method or class), which is never subject to an enclosing instance.
To create a class variable for an inner class, the programmer must place the desired variable in an enclosing class.
does it mean the following?
Code: Select all
class Outer {
Inner inner = new Inner();
class Inner {}
}
or
Code: Select all
class Outer {
static int i;
class Inner {}
}
Re: About Question com.enthuware.ets.scjp.v6.2.189 :
Posted: Fri May 16, 2025 2:13 am
by admin
class Outer {
Inner inner = new Inner();
class Inner {}
}
Re: About Question com.enthuware.ets.scjp.v6.2.189 :
Posted: Fri May 16, 2025 2:23 am
by nkaragulov
admin wrote: ↑Fri May 16, 2025 2:13 am
class Outer {
Inner inner = new Inner();
class Inner {}
}
So what is the relation of the explanation to static members?
What does it mean in the context of the whole paragraph?
Re: About Question com.enthuware.ets.scjp.v6.2.189 :
Posted: Fri May 16, 2025 8:33 pm
by admin
I am sorry, I misspoke. This statement is now meaningless and must be removed. It was actually take from here:
https://jcp.org/aboutJava/communityproc ... lasses.pdf
But it is no more valid because an inner class (i.e. a non-static nested class) can have static members even if they are not final. For example:
Code: Select all
class Outer {
class Inner {
static int i;
}
}
In the above example, static int i is a member of Inner.