Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner classes

 
leena rane
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which statement is true about a non-static inner class? (Sun's
sample)
- 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
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
- It is accessible from any other class.

Not if it is private...
 
leena rane
Ranch Hand
Posts: 280
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Leena,
I have created a chart about the relationship of innerclasses with others.It is available here
Regards
Gurpreet Sachdeva
For Mock Exams, FAQ and some useful information about Bitshift operator, inner classes, garbage collection,etc please visit: http://www.go4java.20m.com
 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = o1.new 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
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the meaning of public, protected, private and default access modifiers, whenever possible, are the same for nested or top level classes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic