This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inner Classes" Watch "Inner Classes" New topic
Author

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

Regards
-venkat
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();
inner.display();
}
}
}

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

Joined: Aug 31, 2004
Posts: 11343

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

http://www.javaranch.com/campfire/StoryInner.jsp


"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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Classes
 
Similar Threads
What is a class file with $ symbol in the name
Most Difficult Part?
Re:inner classes
Native Method
Why final keyword is accepted with private?