1

I am learning about static nested classes and I came across the following:

A static nested class is declared inside another class with the static keyword or within a static context of that class.

What I cannot understand is what does it mean when it says "or within a static context of that class."

If possible can someone give me a few line example. I don't understand what it means "static context".

3 Answers 3

1

I think it means a class inside a static initializer:

public class OuterClass
{
    static
    {
        class InnerClass
        {

        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I am sorry to not know but what is a static initializer. I have never seen anything like that?
0

when they say static context means anything declared static, it can be nested inside a nested static class, enum or interface (enums and interfaces are static by default and can only be declared inside a top level context).

Also, you can declare inner classes within static methods , static declarations or static blocks, this referring to local classes or anonymous classes.

Regards

Comments

0

A nested class that is declared static is called a static nested class. Memory to the objects of any static nested classes are allocated independently of any particular outer class object. A static nested class use the instance variables or methods defined in its enclosing class only through an object reference. A static nested class interacts with the instance members of its outer class or any other class just like a top-level class.

Given below is the syntax of the Static nested class that defines static nested class having keyword static in the outer class.

      class OuterClass {
           ....
         static class StaticNestedClass {
           ....
        }
         class InnerClass {
          .... }
       }

Static nested classes can be accessed by using the enclosing class name: OuterClass.StaticNestedClass

If we want to make an object of the static nested class then we have to write down the following code:

      OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

Here is an example of the Static nested class that processes of accessing the instance of the outer class inside the inner class. The example creates an "Outer" class with an instance x of value 100 and after that we call this value in inner class method check. Apart from that the example also creates another function check and call the inner class check() method inside it. When the example calls the check() with Outer class, it shows the value of Outer class instance x.

Here is the code of the Example :

Outer.java

    import java.lang.*;

    public class Outer{

     int x = 100;

    class Inner{

     int x = 200;

    public void check(){

    System.out.println("Value of x is: "+ Outer.this.x );

     }

     }

    public void check(){

         new Inner().check();

    }

     public static void main(String args[]){

       new Outer().check();

    }

     }

go to the link http://littletutorials.com/2008/03/06/static-nested-classes/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.