File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes JQ+ Static Member On Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "JQ+ Static Member On Inner Class" Watch "JQ+ Static Member On Inner Class" New topic
Author

JQ+ Static Member On Inner Class

Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
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

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Dear Jane...,
Thank's for the info,
that's realy help.
stevie
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
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

Joined: May 02, 2001
Posts: 16

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

Joined: Dec 14, 2000
Posts: 38
Hey !!! What's the problem anyways ...
It is working fine.
Cheers
Tejas Kansara.
Prakshi Chopra
Greenhorn

Joined: Mar 05, 2001
Posts: 10
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
 
Consider Paul's rocket mass heater.
 
subject: JQ+ Static Member On Inner Class