File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner Classes" Watch "Inner Classes" New topic

Inner Classes

Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
Hi All
Can anybody helpout me in knowing "inner classes" with an example

Sheetal Kaul
Ranch Hand

Joined: Nov 29, 2004
Posts: 47
It is possible to define a class within another class, such classes are known as Nested Classes, There are two types of nested classes : static and non-static. The most important type of nested class is Inner Class. An inner class is a non-static nested class. It has access to all of the variables and methods of its outer class and may refer to them directly in the same way that the other non-static members of the outer class do. Thus, an Inner class is fully within the scope of its enclosing class.

Following program illustrates how to define and use an inner class. The class named Outer has one instance variable named outer_x, one instance method named test(), and defines one Inner class called Inner.

class Outer {
int outer_x = 100;
void test() {
for (int i=0; i<10; i++) {
class inner {
void display() {
System.out.println("Display : outer_x = " + outer_x );
Inner inner = new Inner();

class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
marc weber

Joined: Aug 31, 2004
Posts: 11343

Here's an excellent JavaRanch article on inner classes...

"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
I agree. Here's the link:
subject: Inner Classes
It's not a secret anymore!