Java Program to illustrates Use of Static Inner Class
Inner class means one class that is a member of another class. There are basically four types of inner classes in java.
- Nested Inner class
- Method Local inner classes
- Anonymous inner classes
- Static nested classes
Java also allows a class to be defined within another class. These are called Nested Classes. The class in which the nested class is defined is known as the Outer Class. Unlike top-level classes, Inner classes can be Static. Non-static nested classes are also known as Inner classes. In this article, we will implement a static inner class in java programs.
Example 1: An Instance of the static inner class is created and its method is called later.
Java
public class Gfg {
static String msg = "GeeksForGeeks" ;
static class InnerClass {
public void display()
{
System.out.println( "Welcome to " + msg);
}
}
public static void main(String[] args)
{
InnerClass instance = new InnerClass();
instance.display();
}
}
|
Output:
Welcome to GeeksForGeeks
Example 2: Creating an instance of the outer class and calling the static inner class method.
Java
public class GFG {
static String msg = "GeeksForGeeks" ;
static class InnerClass {
public void display()
{
System.out.println( "Welcome to " + msg);
}
}
public static void main(String[] args)
{
Gfg.InnerClass instance = new Gfg.InnerClass();
instance.display();
}
}
|
Output:
Welcome to GeeksForGeeks
Last Updated :
08 Dec, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...