Static Nested Inner class in Java|Interview Question| Selenium WebDriver



Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes.Inner Class in JAVA is a class embedded inside a another Class which is called as outer class. It is also called as Nested Class. Inner class is not accessible from outer class. It is required to create an object of the inner class in outer class.
Advantage:
The inner class can access the methods and variables of the outer class without creating objects of outer class.


Static Inner Class in JAVA:
An inner class can be declared as static, in which we can access the inner class from outer class without creating an object of the outer class.

Advantage:
The purpose of the static inner class makes the code more readable and easily under-stable.

ById is an static nested inner class in By Class

Conclusion:
static nested inner classes are mainly useful for more readability.




1 comment on “Static Nested Inner class in Java|Interview Question| Selenium WebDriver

  1. govardhan

    Hi SIr, Could u pls clarify this.

    Advantage:
    The inner class can access the methods and variables of the outer class without creating objects of outer class.

    But OUter class object is created in static area of Outer class main() method.
    public static void main(String args[])
    {
    OuterClass outer = new OuterClass();
    OuterClass.InnerClass inner = outer.new InnerClass();
    inner.displayInner();
    System.out.println(inner.innerVar);
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *