File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between inner and outer classes

 
aka
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

See Getting in touch with your inner class, and let us know if you have further questions.
 
Joe Wolfe
Ranch Hand
Posts: 87
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David McCombs
Ranch Hand
Posts: 212
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found out that inner classes cannot have static declarations.


An inner class can have static members if the inner class is static.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic