- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Nested and Inner Classes
In Java, it is possible to define a class within another, such classes are known as nested classes.
The scope of a nested class is bounded through the scope of its enclosing class. Thus, if a class B is defined within a class A, then B doesn't exist independently of class A.
A nested class has access to the members, including private members, of the class in which it is nested. On the other hand, the enclosing class doesn't have access to the members of the nested class. A nested class which is declared directly in its enclosing class scope is a member of its enclosing class. It is also possible to declare a nested class which is local to a block.
There are two types of nested classes given here :
- static
- non-static
Java Static Nested Class
A static nested class is the one which has the static modifier applied. As it is static, so it must access the non-static members of its enclosing class by an object i.e., it can't refer to non-static member of its enclosing class directly. As of this restriction, the static nested classes are rarely used.
Java Non-Static Nested Class - Inner Class
The most crucial types of nested class is the inner class. And an inner class is a non-static nested class. It has access to all of the variables and methods of its outer class and might refer to them directly in same way that the other non-static members of the outer class do.
Here this example program illustrates how to define and use an inner class. The class Outer has one instance variable named outer_x, and one instance method named test(), and defines one inner class called Inner.
/* Java Program Example - Java Nested and Inner Classes * This program demonstrates an inner class in Java */ class Outer { int outer_x = 100; void test() { Inner inner = new Inner(); inner.display(); } /* this is an inner class */ class Inner { void display() { System.out.println("display : outer_x = " + outer_x); } } } class JavaProgram { public static void main(String args[]) { Outer outer = new Outer(); outer.test(); } }
When the above Java program is compile and executed, it will produce the following output:
In the previous program, an inner class named Inner is defined within the scope of the class Outer. Therefore, any code in the class Inner can directly access the variable outer_x. An instance method named display() is defined inside Inner. And this display() method displays outer_x on the standard output stream. The main() method of JavaProgram creates an instance of the class Outer and invokes its method named test(). That method creates an instance of class Inner and the display() method is called.
It is important to understand that an instance of Inner can be created only in the context of the class Outer. The Java compiler produces an error message otherwise. In general, an inner class instance is often created by the code within its enclosing scope, as the example does.
As explained, an inner class has access to all of the members from its enclosing class, but the reverse is untrue. Members of the inner class are known only within the scope of the inner class and may not be used through the outer class. For instance, consider the following example program :
Java Nested and Inner Classes Example
Here is an example program in Java, uses nested and inner class in Java:
/* Java Program Example - Java Nested and Inner Classes. * This program produce an error message. * For instance, in BlueJ, it will produce * cannot find symbol - variable y * So this program will not compile. */ class Outer { int outer_x = 100; void test() { Inner inner = new Inner(); inner.display(); } /* this is an inner class */ class Inner { int y = 10; // y is local to Inner void display() { System.out.println("display : outer_x = " + outer_x); } } void showy() { System.out.println(y); // error!, y not known here !! } } class JavaProgram { public static void main(String args[]) { Outer outer = new Outer(); outer.test(); } }
Here, y is declared as an instance variable of Inner. Thus, it isn't known outside of that class and it can't be used by showy().
« Previous Tutorial Next Tutorial »