1

I have read the posts on differences between static nested classes and inner classes. My question is more of an idiomatic java one.

My requirement was to demonstrate various concepts like inheritance using a Single piece of code in Java. A single class file, so that it can be studied top-down and verified by running in sandbox like ideone - http://ideone.com/jsHePB

class SOFieldAccessQuestion {

    static class B {
        int vf;
        static int sf;

        B(int i) {
            vf = i;
            sf = i + 1;
        }
    }

    static class C extends B {
        int vf;
        static int sf;
        C(int i) {
            super(i+20);
            vf = i;
            sf = i + 2;
        }
    }

    public static void main(String[] args) {

        // Is it Okay to create an instance of static nested class?

        C c1 = new C(100);
        B b1 = c1;

        System.out.println("B.sf = " + B.sf + ", b1.sf = " + b1.sf);
        System.out.println("C.sf = " + C.sf + ", c1.sf = " + c1.sf);

        System.out.println("b1.vf = " + b1.vf);
        System.out.println("c1.vf = " + c1.vf);
    }

}

My question is, is it okay to create an instance of a static nested class like above? I fear that I am ignoring the meaning of "static" here. How should I refer and acknowledge the concept that Java allows me to create objects of static class.

If I don't create a static class of B or C, then I can't use them inside public static void main method and may not execute the program with a main entry point and thus I am forced to do it.

3 Answers 3

3

is it okay to create an instance of a static nested class like above?

Yes, it is. For example, look at the various nested classes in java.util.Collections.

How should I refer and acknowledge the concept that Java allows me to create objects of static class.

A static class within a class is called a nested class. The containing concept is known as a member class, which is (more or less) like any other member of a class. Being a static member, the nested class belongs to the containing class, not to its instances.

If I don't create a static class of B or C, then I can't use them inside public static void main method and may not execute the program with a main entry point and thus I am forced to do it.

You can still declare your classes outside your SOFieldAccessQuestion but in the same .java file. They just can't be public.

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

1 Comment

Thanks for your answer. Clarifies my confusion around this concept.
0

Ah, I missed the CTOR. Here it is with the correct CTOR:

SOFieldAccessQuestion.B b = new SOFieldAccessQuestion.B( 0 );
SOFieldAccessQuestion.C c = new SOFieldAccessQuestion.C( 1 );

2 Comments

Thanks. This is when I am using B and C from a class other than SOFieldAccessQuestion.Here I am using them within SOFieldAccessQuestion main entry point and thus want use them as B and C only.
This sort of thing is very common. For example, Map has a class that it uses to track pairs of keys and values, called Entry. You name and create that class as "Map.Entry".
0

Yes, it's okay.

From: http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

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. Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

Static nested classes are accessed using the enclosing class name:

OuterClass.StaticNestedClass

For example, to create an object for the static nested class, use this syntax:

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

What this means is that the class is declared in a static context relative to the containing class. If you have a class declared like:

class Container { 
  private int x; 
  public static class Inner {
    private int y = x; // Invalid, member not in scope
  }
}

It would not work, because static nested classes to not have access to the containing class's private members.

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.