my dog learned polymorphism
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner classes" Watch "inner classes" New topic

inner classes

leena rane
Ranch Hand

Joined: Aug 13, 2001
Posts: 280
Which statement is true about a non-static inner class? (Sun's
- It is accessible from any other class.
- It can only be instantiated in the enclosing class.
- It can access private instance variables in the enclosing object.
Though i know answer is 3 i would like someone to discuss about first two options.Thanx in advance
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
right 3 is true.
reg. first point: the (non-static) inner class is not accessible without an instance of the enclosing class
reg. second point: an (non-static) inner class may be instantiated anywhere provided we have an instance of the enclosing class
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

[Blog] [Blogroll] [My Reviews] My Linked In
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9056
- It is accessible from any other class.

Not if it is private...

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
leena rane
Ranch Hand

Joined: Aug 13, 2001
Posts: 280
Originally posted by Marilyn deQueiroz:
- It is accessible from any other class.

Not if it is private...

So the meaning of access modofiers to inner classes are same as those to top-level classes?
Gurpreet Sachdeva
Ranch Hand

Joined: Feb 20, 2001
Posts: 90
Hello Leena,
I have created a chart about the relationship of innerclasses with others.It is available here
Gurpreet Sachdeva
For Mock Exams, FAQ and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit:

Regards<BR>Gurpreet Sachdeva<P>For Mock Exams, FAQ, Exam tips and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: <A HREF="" TARGET=_blank rel="nofollow"></A>
Nisheeth Kaushal
Ranch Hand

Joined: Jul 20, 2001
Posts: 87
Hello Leena,
Meaning of Access Modifier for Top - Level Class and Inner Class are not same always. Although to know their behaviour u can consider them as a method of top-level class.
and Inner classes are non-static classes defined within other classes
Here is a code example for Inner class and their creation.
class Outer {
class Inner {} // class definition within the
// the body of class Outer
the compiled class files for the above are: Outer.class and Outer$Inner.class
So, leena we can consider that Inner class just uses naming convention for their nomenclature.
the Inner class type is: Outer.Inner
and Instances of inner classes can be created in a number of ways
Create an Outer class object:
Outer o1 = new Outer();
Then create an Inner class object:
Outer.Inner i1 = Inner();
but u cann't create an object of Inner class without instantiating the outer class.

Creating the inner class directly:
Outer.Inner i2 = new Outer().new Inner();

Creating one from within the outer class constructor
class Outer {
Outer() {
new Inner();
inner classes may have no declared access modifier, defaulting the class access to package
or, inner classes may be declared public, protected, private, abstract, static or final
class Outer {
public class PublicInner{}
protected class ProtectedInner {}
private class PrivateInner{}
abstract class AbstractInner {}
final class FinalInner {}
static class StaticInner {}
each instance of a non-static inner class is associated with an instance of their outer class
static inner classes are a special case.
inner classes may not declare static initializers or static members unless they are compile time constants ie static final var = value;
you cannot declare an interface as a member of an inner class; interfaces are never inner
inner classes may inherit static members
the inner class can access the variables and methods declared in the outer class
to refer to a field or method in the outer class instance from within the inner class, use Outer.this.fldname
Hope this helps,
Nisheeth Kaushal
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I think the meaning of public, protected, private and default access modifiers, whenever possible, are the same for nested or top level classes.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: inner classes
jQuery in Action, 3rd edition