• 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

Difference between inner and outer classes

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...


 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic