How to use and access the inner class in java

By Saravanan Viewed: 31769 times Emailed: 119 times Printed: 132 times Bookmark and Share



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



Comments(4)


1. Helpful..

By: syam at 2009-12-29 07:14:05
2. 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!

By: lee smith at 2010-01-17 10:59:23
3. What a wonderful site for beginners? Now I can practice in my own using your examples. Is it free?

By: Kafedha Rwezaula at 2010-06-03 04:39:59
4. How can I submit my query in here?

By: Kafedha Rwezaula at 2010-06-04 01:32:30

Your name (required):


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


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2010-09-02]Steps in using verisign certificate with Glassfish appserver
[2010-08-02]emulator 0 terminated while waiting for it to register!
[2010-08-02]Cannot run program "C:\Program Files\Java\jre6\bin\javac.exe": CreateProcess error=2, The system cannot find the file specified
[2010-08-01]Step by Step guide to setup freetts for Java
[2010-07-31]Speech Packages available for Java API
[2010-07-31]Tutorial on setting up freetts with maven
[2010-07-31]package com.sun.speech.freetts does not exist.
[2010-07-31]Text to Speech conversion program in Java
[2010-07-31]How to create wav file using freetts
[2010-07-31]How to set the width of a Text element in JavaFX?
[2010-07-31]Major components of FxObjects in JavaFX
[2010-07-03]Using the AWS SDK for Java in Eclipse
[2010-07-03]Using the AWS SDK for Java
[2010-01-01]Converting properties using PropertyEditors and Other Spring features worth mentioning
[2010-01-01]How to create an array and method in JSP

More Latest News

Most Viewed Articles (in last 30 days)
How to use ArrayList in Java
XML and Java - Parsing XML using Java Tutorial
How to use Iterator in Java
Using substring( ) in Java
How to Send SMS using Java Program (full code sample included)
FileReader and FileWriter example program in Java
indexOf( ) and lastIndexOf( ) in Java
Using StringTokenizer in Java
HashMap example in Java
wait(), notify() and notifyAll() in Java - A tutorial
Abstract classes in Java
Method Overriding in Java
compareTo( ) in Java
Method Overloading (function overloading) in Java
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
Most Emailed Articles (in last 30 days)
Components of program
How to Send SMS using Java Program (full code sample included)
XML and Java - Parsing XML using Java Tutorial
Why java is important to the Internet
How to use ArrayList in Java
Execute system commands in a Java Program
FileReader and FileWriter example program in Java
Recursion in java
indexOf( ) and lastIndexOf( ) in Java
Method Overloading (function overloading) in Java
What is Java?
compareTo( ) in Java
History of Object
Sample Java Script that displays a movable clock
How to use Iterator in Java