Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JQ+ Static Member On Inner Class

 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q#ID : 958227370580
which of the following statements regarding inner classes are true ?
1. A non static inner class may have static members
2. Anonymous inner classes cannot have any 'extends' or 'implements' clause.
3. Anonymous inner classes can only be created for interfaces
4. Anonymous inner classes can never have initialization parameters.
5. Anonymous inner classes cannot be static.
the answer given is : 1,2,5
I tought the only 2 and 5 are correct,
for answer 1, JQ+ said :
Option Comment : "If you make them final"
General Comment :
"...Erlier, Non static inner clasess were not allowed to have static fields at all. THIS RULE IS NOW MODIFIED, and the new rule says that :
The third paragraph of the section members that can be marked static is amended to make an exception and allow inner classes to declare static FINAL fields that are compile time constants as members.
Ex.
public class Outer {
class inner {
static final int k = 0;
}
}
IS IT TRUE ???
I tried, but it give me compile error !!
thank's
stevie
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stevie,
An inner class with a static final field compiles ok with JDK 1.3. Which compiler version are you using?
It is true, see the JLS§8.1.2 Inner Classes and Enclosing Instances

An inner class is a nested class that is not explicitly or implicitly declared static. Inner classes may not declare static initializers (�8.7) or member interfaces. Inner classes may not declare static members, unless they are compile-time constant fields (�15.28).

You can declare a static final field but NOT a static final method.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Jane...,
Thank's for the info,
that's realy help.
stevie
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Jane...,
I tried with JDK 1.3 but it still didn't compile.
here is my code :
public class MyClass {
class A{
static final int x = 10;
}
public static void main(String[] args) { }
}
the compiler says :
only member of interface and top level classess can be static
help me Jane...
thank's
stevie
 
Umesh Joglekar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I compiled following with JDK1.3 without any problems
public class MyClass {
class A{
static final int x = 10;
}
public static void main(String[] args) { }
}

 
Tejas Kansara
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey !!! What's the problem anyways ...
It is working fine.
Cheers
Tejas Kansara.
 
Prakshi Chopra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey What's wrong with option
4. Anonymous inner classes can never have initialization parameters.
we can definitely have initialization parameters in
INITIALIZING BLOCKS inside Inner Classes
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic