aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Inner Class" Watch "Inner Class" New topic
Author

Inner Class

Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
Hi all,
How we create new object from non-static inner class and static inner class (nested class) ?
thanks
daniel
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Outer. Inner i = new Outer.Inner();
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Non-static inner class: you need an instance of the outer class:

Static inner classes (top level nested classes): you don't need an instance of the outer class:

HIH
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Wale Adegabi
Greenhorn

Joined: Sep 28, 2001
Posts: 3
Please could you tell me if these two lines mean the same thing?
Outer.Inner i = new Outer.Inner();// A
Outer.Inner i = new Outer().new Inner(); // B
If they are could you please tell me which is most appropriate version?
Thanks
'Wale

------------------
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
First of all, remember:
a)An instance of an inner class needs an instance of the containning class for being created.
b)A non-inner (static) nested class doesn't need it.
c)Within an instance method "this" points to the object over which the invocation was made. Within a constructor this points to the object being constructed.
Now
Outer.Inner i = new Outer.Inner();// A

If Inner was an inner class how would the Inner instance obtain such reference? The only way is if this invocation took place whitin an instance method of the containing class (or its subclasses)or whithing a constructor of the containing class (or its subclasses). Why? Because you are invoking new Outer.Inner() over "this" and only in those contexts "this" is an instance of a class container of Inner.
If Inner was a static nested class A could be placed in any static method of the containning class (or its subclasses)or
other class not being the containning one. But "new Inner()" could be placed only within a static method of the containning class or its subclasses.
While
Outer.Inner i = new Outer().new Inner(); // B
If Inner was an inner class this expression could occur anywhere because you are providing to the instance of Inner the instance of the containning class that needs. But "new Inner()" would compile only if takes place in an instance method or constructor of the containning class (or its subclasses). The reason is because only in these contexts the hidden argument "this" points to an instance of the containning class (or its subclasses).
If Inner was static nested, well, you can invoke a static method using an instance of the class isn't it? This is the same.

hope helps


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Inner Class
 
Similar Threads
Top-level classes
Question about static class
is inner class can be static . what is the purpose of inner class ??
doubt on Inner class
no static method in inner class - why ?