wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Difference between inner and outer classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between inner and outer classes" Watch "Difference between inner and outer classes" New topic
Author

Difference between inner and outer classes

aka
Greenhorn

Joined: Mar 25, 2007
Posts: 1
Hello people...i'm new to this forum and i'm new to java as well...

Can somebody please tell me what are the differences between an inner class and an outer class???
I found out that inner classes cannot have static declarations. Are there any other significant differences???

Thanks in advance
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!

See Getting in touch with your inner class, and let us know if you have further questions.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Joe Wolfe
Ranch Hand

Joined: Apr 25, 2007
Posts: 87
inner class is inside another class.
I recall private data from the outer class is not available within an inner class but this may not be correct.
All data and methods of outer class are availble to inner class.
The inner class is not known to other classes. Good way to hide stuff. Outer class knows about it but no other.


If not now then never.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Joe Wolfe:
...I recall private data from the outer class is not available within an inner class but this may not be correct...

Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
The main feature of an inner class is its capability to access even private members of the outer class. Inner classes were included in java, (after many altercated discussions I guess) to enhance inter class ties. There might be a case when your class is specific enough to deserve an entirely new type, but on the other hand, it is tightly coupled with another class. In this condition, inner classes can be used. Though, they have their own limitations. Please refer the documentation on the link specified above.

Sid,

[ May 03, 2007: Message edited by: Sidd Kulk ]
Gayal Rupasinghe
Greenhorn

Joined: Sep 27, 2005
Posts: 1
Inner classes are used primarily to implement helper classes. If you plan on handling user-interface events, you'll need to know about using inner classes because the event-handling mechanism makes extensive use of them.

There are two additional types of inner classes. You can declare an inner class within the body of a method. Such a class is known as a local inner class. You can also declare an inner class within the body of a method without naming it. These classes are known as anonymous inner classes.


G.N.R
David McCombs
Ranch Hand

Joined: Oct 17, 2006
Posts: 212
I found out that inner classes cannot have static declarations.


An inner class can have static members if the inner class is static.


"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by David McCombs:
...if the inner class is static.

This raises the old terminology question. A nested class can be static, but according to the JLS...
An inner class is a nested class that is not explicitly or implicitly declared static... Inner classes may not declare static members, unless they are compile-time constant fields..."

(Ref: 8.1.3 Inner Classes and Enclosing Instances.)
[ May 04, 2007: Message edited by: marc weber ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between inner and outer classes