• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inner classes

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
- It is accessible from any other class.

Not if it is private...
 
leena rane
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic