About Question com.enthuware.ets.scjp.v6.2.189 :

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

Moderator: admin

Post Reply
TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

About Question com.enthuware.ets.scjp.v6.2.189 :

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

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

Re: About Question com.enthuware.ets.scjp.v6.2.189 :

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

TwistedLizard
Posts: 57
Joined: Sat Mar 01, 2014 1:48 pm
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.189 :

Post by TwistedLizard »

ok. Thanks for your help.

nkaragulov
Posts: 17
Joined: Mon Sep 16, 2024 1:49 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.189 :

Post 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 {}
}

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

Re: About Question com.enthuware.ets.scjp.v6.2.189 :

Post by admin »

class Outer {
Inner inner = new Inner();
class Inner {}
}

nkaragulov
Posts: 17
Joined: Mon Sep 16, 2024 1:49 am
Contact:

Re: About Question com.enthuware.ets.scjp.v6.2.189 :

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

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

Re: About Question com.enthuware.ets.scjp.v6.2.189 :

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

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 87 guests