aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question from JQPlus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A question from JQPlus " Watch "A question from JQPlus " New topic
Author

A question from JQPlus

Ashish Chande
Ranch Hand

Joined: Nov 26, 2000
Posts: 51
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.)

Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
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.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3293
    
    7
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


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
jing zhao
Greenhorn

Joined: Feb 21, 2001
Posts: 10
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();
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question from JQPlus