Nested Classes in Java
In java, it is possible to define a class within another class, such classes are known
as nested classes. They enable you to logically group classes that are only used in one
place, thus this increases the use of encapsulation, and create more readable and
maintainable code.
The scope of a nested class is bounded by the scope of its enclosing class. Thus
in above example, class NestedClass does not exist independently of
class OuterClass.
A nested class has access to the members, including private members, of the
class in which it is nested. However, reverse is not true i.e. the enclosing class
does not have access to the members of the nested class.
A nested class is also a member of its enclosing class.
As a member of its enclosing class, a nested class can be
declared private, public, protected, or package private(default).
Nested classes are divided into two categories:
1. static nested class : Nested classes that are declared static are called static
nested classes.
2. inner class : An inner class is a non-static nested class.
Syntax:
class OuterClass
{
...
class NestedClass
{
...
}
}
Static nested classes
As with class methods and variables, a static nested class is associated with its outer
class. And like static class methods, a static nested class cannot refer directly to
instance variables or methods defined in its enclosing class: it can use them only
through an object reference.
They are accessed using the enclosing class name.
[Link]
For example, to create an object for the static nested class, use this syntax:
[Link] nestedObject =
new [Link]();
filter_none
edit
play_arrow
brightness_4
// Java program to demonstrate accessing
// a static nested class
// outer class
class OuterClass
{
// static member
static int outer_x = 10;
// instance(non-static) member
int outer_y = 20;
// private member
private static int outer_private = 30;
// static nested class
static class StaticNestedClass
{
void display()
{
// can access static member of outer class
[Link]("outer_x = " + outer_x);
// can access display private static member of outer class
[Link]("outer_private = " + outer_private);
// The following statement will give compilation error
// as static nested class cannot directly access non-static membera
// [Link]("outer_y = " + outer_y);
}
}
}
// Driver class
public class StaticNestedClassDemo
{
public static void main(String[] args)
{
// accessing a static nested class
[Link] nestedObject = new [Link]();
[Link]();
}
}
Output:
outer_x = 10
outer_private = 30
Inner classes
To instantiate an inner class, you must first instantiate the outer class. Then, create the
inner object within the outer object with this syntax:
[Link] innerObject = [Link] InnerClass();
There are two special kinds of inner classes :
1. Local inner classes
2. Anonymous inner classes
filter_none
edit
play_arrow
brightness_4
// Java program to demonstrate accessing
// a inner class
// outer class
class OuterClass
{
// static member
static int outer_x = 10;
// instance(non-static) member
int outer_y = 20;
// private member
private int outer_private = 30;
// inner class
class InnerClass
{
void display()
{
// can access static member of outer class
[Link]("outer_x = " + outer_x);
// can also access non-static member of outer class
[Link]("outer_y = " + outer_y);
// can also access private member of outer class
[Link]("outer_private = " + outer_private);
}
}
}
// Driver class
public class InnerClassDemo
{
public static void main(String[] args)
{
// accessing an inner class
OuterClass outerObject = new OuterClass();
[Link] innerObject = [Link] InnerClass();
[Link]();
}
}
Output:
outer_x = 10
outer_y = 20
outer_private = 30
Difference between static and inner(non-static nested) classes
Static nested classes do not directly have access to other members(non-static
variables and methods) of the enclosing class because as it is static, it must
access the non-static members of its enclosing class through an object. That is, it
cannot refer to non-static members of its enclosing class directly. Because of this
restriction, static nested classes are seldom used.
Non-static nested classes (inner classes) has access to all members(static and
non-static variables and methods, including private) of its outer class and may refer
to them directly in the same way that other non-static members of the outer class
do.
This article is contributed by Gaurav Miglani. If you like GeeksforGeeks and would like
to contribute, you can also write an article using [Link] or mail
your article to contribute@[Link]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed above.
Inner class in java
Inner class means one class which is a member of another class. There are basically
four types of inner classes in java.
1) Nested Inner class
2) Method Local inner classes
3) Anonymous inner classes
4) Static nested classes
Nested Inner class can access any private instance variable of outer class. Like any
other instance variable, we can have access modifier private, protected, public and
default modifier.
Like class, interface can also be nested and can have access specifiers.
Following example demonstrates a nested class.
class Outer {
// Simple nested inner class
class Inner {
public void show() {
[Link]("In a nested class method");
}
}
}
class Main {
public static void main(String[] args) {
[Link] in = new Outer().new Inner();
[Link]();
}
}
Output:
In a nested class method
As a side note, we can’t have static method in a nested inner class because an inner
class is implicitly associated with an object of its outer class so it cannot define any
static method for itself. For example the following program doesn’t compile.
class Outer {
void outerMethod() {
[Link]("inside outerMethod");
}
class Inner {
public static void main(String[] args){
[Link]("inside inner class Method");
}
}
}
Output:
Error illegal static declaration in inner class
[Link] public static void main(String[] args)
modifier ‘static’ is only allowed in constant
variable declaration
An interface can also be nested and nested interfaces have some interesting properties.
We will be covering nested interfaces in the next post.
Method Local inner classes
Inner class can be declared within a method of an outer class. In the following example,
Inner is an inner class in outerMethod().
class Outer {
void outerMethod() {
[Link]("inside outerMethod");
// Inner class is local to outerMethod()
class Inner {
void innerMethod() {
[Link]("inside innerMethod");
}
}
Inner y = new Inner();
[Link]();
}
}
class MethodDemo {
public static void main(String[] args) {
Outer x = new Outer();
[Link]();
}
}
Output
Inside outerMethod
Inside innerMethod
Method Local inner classes can’t use local variable of outer method until that local
variable is not declared as final. For example, the following code generates compiler
error (Note that x is not final in outerMethod() and innerMethod() tries to access it)
class Outer {
void outerMethod() {
int x = 98;
[Link]("inside outerMethod");
class Inner {
void innerMethod() {
[Link]("x= "+x);
}
}
Inner y = new Inner();
[Link]();
}
}
class MethodLocalVariableDemo {
public static void main(String[] args) {
Outer x=new Outer();
[Link]();
}
}
Output:
local variable x is accessed from within inner class;
needs to be declared final
Note : Local inner class cannot access non-final local variable till JDK 1.7. Since JDK
1.8, it is possible to access the non-final local variable in method local inner class.
But the following code compiles and runs fine (Note that x is final this time)
class Outer {
void outerMethod() {
final int x=98;
[Link]("inside outerMethod");
class Inner {
void innerMethod() {
[Link]("x = "+x);
}
}
Inner y = new Inner();
[Link]();
}
}
class MethodLocalVariableDemo {
public static void main(String[] args){
Outer x = new Outer();
[Link]();
}
}
Output-:
Inside outerMethod
X = 98
The main reason we need to declare a local variable as a final is that local variable lives
on stack till method is on the stack but there might be a case the object of inner class
still lives on the heap.
Method local inner class can’t be marked as private, protected, static and transient but
can be marked as abstract and final, but not both at the same time.
Static nested classes
Static nested classes are not technically an inner class. They are like a static member of
outer class.
class Outer {
private static void outerMethod() {
[Link]("inside outerMethod");
}
// A static inner class
static class Inner {
public static void main(String[] args) {
[Link]("inside inner class Method");
outerMethod();
}
}
Java Command Line Arguments
The java command-line argument is an argument i.e. passed at the time of running the java
program.
The arguments passed from the console can be received in the java program and it can be
used as an input.
So, it provides a convenient way to check the behavior of the program for the different
values. You can pass N (1,2,3 and so on) numbers of arguments from the command prompt.
Simple example of command-line argument in java
1. class CommandLineExample{
2. public static void main(String args[]){
3. [Link]("Your first argument is: "+args[0]);
4. }
5. }
1. compile by > javac [Link]
2. run by > java CommandLineExample sonoo
Output: Your first argument is: sonoo
example
In this example, we are printing all the arguments passed from the command-line. For this purpose, we hav
using for loop.
class A{
public static void main(String args[]){
for(int i=0;i<[Link];i++)
[Link](args[i]);
}
}
1. compile by > javac [Link]
2. run by > java A sonoo jaiswal 1 3 abc
Output: sonoo
jaiswal
1
3
abc