Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access modifiers inside a private static nested class in Java

I have a "private static" nested class in Java. What is the significance of access modifiers for fields and methods inside this class? I've tried both public and private with no effect on my application.

public class MyList<T>{
    private static class Node{ // List node
        private Object item;
        private Node next;
        private Node prev;

        private Node(Node next){
            this.next = next;
        }

        private static Node doStuff(){}
    }
}
like image 608
Roman Avatar asked Nov 02 '10 06:11

Roman


People also ask

Which access modifiers we can give to nested class?

A nested class can be declared with any access modifier, namely private, public, protected, internal, protected internal, or private protected.

How do I access static nested classes?

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. To instantiate an inner class, you must first instantiate the outer class.

Can nested classes access private members in Java?

A nested class is a member of its enclosing class. Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.

Can the modifiers public protected private and static be used for inner classes?

You can use the same modifiers for inner classes that you use for other members of the outer class. For example, you can use the access specifiers private , public , and protected to restrict access to inner classes, just as you use them to restrict access do to other class members.


2 Answers

Because it is a nested class, everything in Node can be accessed by MyList<T>, regardless of access modifier; because it is a private nested class, nothing first declared in Node will be visible outside of MyList<T>.

So, the one case where the access modifier may matter are methods that override a superclass method(e.g. toString()). You can not reduce the visibility of an overridden method. toString() must always be declared public in order for the class to compile.

It should also be noted that when private members are accessed by the outer class, the compiler creates a synthetic method (I believe of package scope). This synthetic method is only visible in the .class file of the nested class.

like image 53
ILMTitan Avatar answered Oct 06 '22 00:10

ILMTitan


Two kinds of nested classes: 1. Static (nested class) and 2. Non-static (also called inner class)

Now, the Outer class, MyList can access all the members of the inner class Node, but you actually use the access specifiers for the members of the class Node (nested class) when you want restrictions of some external class accessing it.

Interesting reads: Source1, Source2

like image 41
zengr Avatar answered Oct 05 '22 23:10

zengr