• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

A question from JQPlus

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends this is Question from JQplus
Question ID :953582884020
Which of these statements are true?
Non static inner class cannot have static members
Objects of top level nested classes can be created without creating instances of outer class
Members variables in any nested class cannot be declared final
Anonymous classes cannot have constructors
Anonymous classes cannot be static
Jqplus Answers: 2, 4, 5
My Answers: 1, 2 & 4
What I think
1. Non Static Inner Class Cannot declare Static methods, Members of enclosing class are not inherited and hence they are not members of Nonstatic innerClass, they are just accessible from there.
2.Objects of Toplevel nested class can be created without creating instances of outer class
This is true.
3.Member variables in any nested class cannot be decalred final
False
4. Anonymous Class cannot have Contructors
True, That's one reason of using instance initializers.
5. Anonyous class cannot be static.
False, They can be if they are defined in a static context(Eg Static Initializer)
So Please ppl in ranch do shed light on it.
Thanks in advance
Ashish

(
Sometimes the answers in the test, really shake u up(Ur Confidence), but that's the Time javaranch come to the rescue and makes us really feel comfortable before the exam.)

 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashish,
Let's take your questions 1 at a time.
1. I think I agree with you the statement given is true. I have tried to make methods or variables static in a non static inner class and the compiler always complains!
2. True
3. False
4. True
5. I agree with JQPlus here: true. Think about this for a minute and you will see the light! An anonymous class always starts with :
new OldClass() { overridden methods };
Where in the world would you put the static modifier? It is not allowed with the new operator. Even if Java let it happen what would you expect to happen? A static class can be instantiated without an outer class instance. What would you call your inner class that can stand alone without an instance of the outer class? Pretty tough to get a handle to a class with no name and no outer instance!
Regards,
Manfred.
 
Enthuware Software Support
Posts: 4907
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The given answers are correct.
. You can have static variable in a non-static inner class. The catch is, it also has to be a constant ( final ).
. As said above, even if the anon. class is defined in a static context, it is not static. Another way to verify it is to use javap.
HTH,
Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
True.(Non-static inner class cannot have static members.)
But non-static inner class can have final static members is also true.
Try the program.
class ToplevelClass{
private static String msg="nonStatic inner class";
public NonStaticInnerClass makeInstance(){
return new NonStaticInnerClass();
}
class NonStaticInnerClass{
//final static var in the nonStatic InnerClass
private final static int staticVar=2;
private String string;
public NonStaticInnerClass(){string=msg;}
public void printMsg(){System.out.println(string);}
}
}
public class Client{
public static void main(String args[]){
ToplevelClass topRef=new ToplevelClass();
ToplevelClass.NonStaticInnerClass innerRef1=topRef.makeInstance();
innerRef1.printMsg();
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic