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

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 57 guests