How to use and access the inner class in java

By: Saravanan  

In this Tutorial we are going to see how to use a named inner class, how to access inner class in Java.

//A named inner class is used.
// This is used to show that it can access non-local variables in the enclosing object.

public class InnerExample
{
static String msg= "Welcome";

public static void main(String[] arg)
{
class InnerClass {
public void doWork()
{
System.out.println(msg);
}
}
InnerClass i = new InnerClass();
i.doWork();
}
}

// Access inner class from outside 

public class InnerExample2
{
public static void main(String[] args)
{
OuterClass outer = new OuterClass();
outer.new InnerClass().welcome();
}
}

class OuterClass
{
public class InnerClass
{
public void welcome()
{
System.out.println("Welcome from InnerClass()");
}
}
}



Archived Comments

1. In the 2nd example ,during the creation on object of innerclass
" outer.new InnerClass

View Tutorial          By: priyanka thakur at 2011-08-29 20:24:16

2. How can I submit my query in here?
View Tutorial          By: Kafedha Rwezaula at 2010-06-04 01:32:30

3. What a wonderful site for beginners? Now I can practice in my own using your examples. Is it free?
View Tutorial          By: Kafedha Rwezaula at 2010-06-03 04:39:59

4. Re: Inner Classes
Aside from allowing access to non-local variables, are there other uses for

View Tutorial          By: lee smith at 2010-01-17 10:59:23

5. Helpful..
View Tutorial          By: syam at 2009-12-29 07:14:05


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial