How to use and access the inner class in java

By: Saravanan Emailed: 1647 times Printed: 2114 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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()");
}
}
}

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(5)


1. View Comment

Helpful..

View Tutorial          By: syam at 2009-12-29 07:14:05
2. View Comment

Re: Inner Classes
Aside from allowing access to non-local variables, are there other uses for inner classes?

Thanks for the web site. It is very helpful!


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

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. View Comment

How can I submit my query in here?

View Tutorial          By: Kafedha Rwezaula at 2010-06-04 01:32:30
5. View Comment

In the 2nd example ,during the creation on object of innerclass
" outer.new InnerClass().welcome(); " what does outer refers to ,is it "outerclass" ?


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

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Saravanan
Converting properties using PropertyEditors and Other Spring features worth mentioning
How to create an array and method in JSP
inheritance in Java
Use List, Set, SortedSet, LinkedHashSet, Map, LinkedHashMap in java
cloneable in Java
Java program for Associate keys with values
Java code for Enumeration and using java.lang.reflect.Array
Insert an element in Array, Search and Sort Array by using java program
Type Casting in Java
How to initialize an Array and how to copy the array
Simple java program to add an element to specified index of Java ArrayList
How to use set, get basic and nested properties for Spring framework
How to access instance from an inner class and accessing outer class variables in java
How to use and access the inner class in java
Java program for Map

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
Read from a COM port using Java program
FileReader and FileWriter example program in Java
append() in Java
Student marks calculation program in Java
Recursion in java
Vector example in Java
XML and Java - Parsing XML using Java Tutorial
Increment and Decrement Operator
Stack example in Java - push(), pop(), empty(), search()
wait(), notify() and notifyAll() in Java - A tutorial
Using StringTokenizer in Java
Sample Java program shows how to write to COM port using Java.
Count number of vowels, consonants and digits in a String in Java
Execute system commands in a Java Program
Most Emailed Articles (in Java)
What is Multithreaded programming?
TCP Server and TCP Client in Java
for loop in java
Read from a COM port using Java program
Calculate gross salary in Java
Calculate average sale of the week in Java
wait(), notify() and notifyAll() in Java - A tutorial
setPriority() and getPriority() in Java
Window Object
constants and variables
Type Casting in Java
Basics of Exception Handling in Java
? - ternary (three-way) operator - in Java
Why java is important to the Internet
Update contents of a file within a jar file