wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes any difference between inner class and nested class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "any difference between inner class and nested class?" Watch "any difference between inner class and nested class?" New topic
Author

any difference between inner class and nested class?

david hu
Ranch Hand

Joined: Jul 20, 2001
Posts: 143
Hi,
I am not clear about inner class and nested class. If there any official definition from sun about inner class and nested class? I mean they are really the same thing or not?
Following is a question from (http://www.geocities.com/SiliconValley/Orchard/9362/java/javacert/newmock.html#)
-----
Which variables can an inner class access from the class which encapsulates it?
A All static variables
B All final variables
C All instance variables
D Only final instance variables
E Only final static variables
Select all correct answers.
The author's answer are ABC. Is it correct?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi David,
I will go straight to the source with this one. JLS section 8.1.2:

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).
To illustrate these rules, consider the example below:
class HasStatic
{
static int j = 100;
}
class Outer
{
class Inner extends HasStatic
{
static final x = 3;// ok - compile-time constant
static int y = 4; // compile-time error, an inner class
}
static class NestedButNotInner
{
static int z = 5; // ok, not an inner class
}
interface NeverInner{}// interfaces are never inner
}
Inner classes may inherit static members that are not compile-time constants even though they may not declare them. Nested classes that are not inner classes may declare static members freely, in accordance with the usual rules of the Java programming language. Member interfaces (�8.5) are always implicitly static so they are never considered to be inner classes.

Regards,
Manfred.
Rosie Nelson
Ranch Hand

Joined: Nov 06, 2001
Posts: 31
Inner classes can access ALL members in the enclosing class.
So in answer to the second part of your question:-
Which variables can an inner class access from the class which encapsulates it?
A All static variables (TRUE)
B All final variables (TRUE)
C All instance variables (TRUE)
D Only final instance variables (FALSE)
E Only final static variables (FALSE)

Rosie
--------
Sun Certified Programmer for Java 2 Platform
david hu
Ranch Hand

Joined: Jul 20, 2001
Posts: 143
Hi Manfred,
Can I ask you a question:
What's JLS, is it a book? what's the name of the book?
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Hi David,
I am not Manfred, but you can download the book as pdf:
The JavaTM Language Specification, Second Edition - Written by the inventors of the technology, this book is the definitive technical reference for the JavaTM programming language. If you want to know the precise meaning of the language's constructs, this is the source for you.

http://java.sun.com/docs/books/jls/index.html
Its not easy-reading but very concise.
Axel
[ January 04, 2002: Message edited by: Axel Janssen ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: any difference between inner class and nested class?