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
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
Dear Jane..., Thank's for the info, that's realy help. stevie
Stevie Kaligis
Ranch Hand
Joined: Feb 04, 2001
Posts: 400
posted
0
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
posted
0
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
posted
0
Hey !!! What's the problem anyways ... It is working fine. Cheers Tejas Kansara.
Prakshi Chopra
Greenhorn
Joined: Mar 05, 2001
Posts: 10
posted
0
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