简体   繁体   中英

Java - Can a class access a private field of its nested class?

as I understand it private fields can only be accessed in the class that they are defined.

I have this code for a rudimentary singly linked list:

public class LinkedList {
    private class Node {
        private String data;
        private Node next;

        private Node(String data, Node nextNode) {
            this.data = data;
            this.next = nextNode;
        }
    }
    private Node head;
    public LinkedList(String data) {
        // Create head node, set next node null
        head = new Node(data, null);
    }
    public void add(String data){
        Node currentNode = head;
        while(currentNode.next != null){
            currentNode = currentNode.next;
        }
        currentNode.next = new Node(data, null);
    }
    public void printList(){
        Node currentNode = head;
        System.out.println(currentNode.data);
        while(currentNode.next != null){
            currentNode = currentNode.next;
            System.out.println(currentNode.data);
        }
    }
}

It appears to run and work just fine, but I am trying to figure out why I can access the 'data' and 'next' fields of class Node in my functions in class LinkedList.

Adding onto this, is a nested class the same thing as a subclass defined with the 'extends' keyword?

Cheers, thanks for the help.

edit: just adding onto this question, in other implementations of linked lists I see people defining two separate classes for node, and list. It made more sense to me for node to be a nested class of linkedlist. If this is not entirely correct please let me know...

but I am trying to figure out why I can access the 'data' and 'next' fields of class Node in my functions in class LinkedList.

Because the Java Language Specification says so

[...] if the member or constructor is declared private , then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

The top level class in your case is LinkedList . The class Node is a member of LinkedList . It therefore makes sense for it ( LinkedList to be able to access any part of it ( Node ).

Adding onto this, is a nested class the same thing as a subclass defined with the 'extends' keyword?

No. For details on nested classes, see the Java tutorials . The extends keyword is used in inheritance. See the corresponding Java tutorial .

It made more sense to me for node to be a nested class of linkedlist. If this is not entirely correct please let me know...

No, what you are doing is fine. Someone using your LinkedList class is not interested in how it's implemented as long as it follows the rules of the data structures or of your specification. Your Node class being nested and private hides those details. This is known as information hiding and encapsulation , which are considered good practice.

Note that your nested class is also considered an inner class. You can read about that in the previous links as well.

why I can access the 'data' and 'next' fields of class Node in my functions in class LinkedList.

You just can. That scoping is intentional in java.

is a nested class the same thing as a subclass defined with the 'extends' keyword?

No, because inside your Node class you can't access this.head . However you can access LinkedList.this.head .

just adding onto this question, in other implementations of linked lists I see people defining two separate classes for node, and list. It made more sense to me for node to be a nested class of linkedlist. If this is not entirely correct please let me know

I think your way is fine. It hides Node, which is an internal implementation detail of your list. To improve it further, you could make it generic, so that add() can take any type. Eg

public class LinkedList <T> {
    //...
    public void add(T value) {
        //...
    }
}

Adding onto this, is a nested class the same thing as a subclass defined with the 'extends' keyword?

Absolutely not. A nested class is class where each instance has a "hidden reference" to the enclosing class instance. Unless declared "static", in which case it is just a kind of gimmick to do .... I'm not sure what,

Frankly, nested classes are the devil's work. They render the code confusing and difficult to read, create implicit dependencies and are generally a PITA to maintain. Avoid.

In truth, this will probably cause religious wars, so "YMMV".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM