This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner classes

 
mohamed hamdy
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13047
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic