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
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Java program to get location meta data from an image
Program using concept of byte long short and int in java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Count number of vowels, consonants and digits in a String in Java
Student marks calculation program in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program