File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner classes" Watch "inner classes" New topic
Author

inner classes

mohamed hamdy
Ranch Hand

Joined: Feb 13, 2003
Posts: 72
look to this question:
Which of the following statements are true
1) An inner class may be defined as static
2) There are NO circumstances where an inner class may be defined as private
3) An anonymous class may have only one constructor
4) An inner class may extend another class
the answer is 1,4
why 2 is wrong?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
Since an inner class is a member of a class, it can be declared private just like any other member. As an exercise, why not write an example?
Bill
Monisha Talwar
Ranch Hand

Joined: Feb 11, 2003
Posts: 102
Which of the following statements are true
1) An inner class may be defined as static
2) There are NO circumstances where an inner class may be defined as private
3) An anonymous class may have only one constructor
4) An inner class may extend another class

1.TRUE: Yes an inner class may be defined as static - then it becomes a top level class - Although then its not an inner class any more, but for the purpose of the exam, its true!
2. FALSE: An inner class [not local or anonymous]may have any access modifier - Public/Protected/Default/Private.
3. FALSE: An anonymous class may not have any constructor since it has no name.
4. TRUE: An inner class can extend another class.
So 1 and 4 are true, 2 and 3 are false.


Hey! It compiles! Ship it!
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
An inner, ie a non-static nested class, is either a member (defined inside the outer class, but not inside a method) or a method-local. A member class can be declared with these optional modifiers: public, protected, private, abstract, final and strictfp. A method-local class, which can be local or anonymous, is by definition inaccessible outside the class, so it makes no sense to declare it with the public, protected or private access modifiers. Note that a local class may be declared abstract, final or strictfp, but an anonymous class has no modifiers.
So, I think that (2) would be correct if referring to method-local classes.
[ April 05, 2003: Message edited by: Roger Chung-Wee ]

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Monisha,
The JLS defines an inner class as "a nested class that is not explicitly or implicitly declared static." So, 1 is wrong.
As for the constructor: you can't have an expicitly declared constructor for an anonymous class, but the compiler provides one. I am fairly sure that it is only one constructor, with parameters dependent on how the anonymous class is created.
Monisha Talwar
Ranch Hand

Joined: Feb 11, 2003
Posts: 102
Can someone confirm how we would answer this question on the exam. Roger I do see your point....Now I am confused
SOMEONE HELPPPP
Monisha Talwar
Ranch Hand

Joined: Feb 11, 2003
Posts: 102
I read this at one of the threads:
http://www.coderanch.com/t/191499/java-programmer-SCJP/certification/Traps-aware-any-SCJP-test

A couple addenda to Ajith's list: static member classes used to be called "top-level nested classes", and were considered top-level, at least according to the original Nested Classes Specification. However this never really caught on, and under the JLS 2nd edition they are no longer considered top-level. I don't think any exam questions ever considered them top-level (or at least, they don't ask about it), so you're safe here.
However, the exam does apparently consider static member classes to be inner classes, even though this is clearly contradicted by the Nested Classes Specification and the JLS2. They don't make a big deal about it - they just call them "static inner classes". So, for the exam, pretend they're inner classes, and remember that in the real world, they aren't.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I believe that these all mean the same thing:
static class
static member class
static nested class
static inner class
top-level nested class
top-level inner class
As far as I can tell, the JLS only uses the term "static class" as in "It is a compile-time error if a static class contains a usage of a non-static member of an enclosing class."
It would be really helpful if Sun could standardise on just one term. :roll:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner classes