0

I was looking at source code of JDK8's HashMap Implementation. I noticed Hashmap is uses a array of Nodes.

transient Node<K,V>[] table;

There is a Node Class defined inside the HashMap Class. Node Class is declared as a static class. Below is the code for Node Class

 static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;

        Node(int hash, K key, V value, Node<K,V> next) {
            this.hash = hash;
            this.key = key;
            this.value = value;
            this.next = next;
        }

        /*
        Some other methods
        .
        .
        .
        .
        .
        */


    }

I would like to know if the class has static keyword, how are we able to create object of this static class ? What is the significance of creating a static class if we are creating objects for this class.( Static is something that is not associated with instances )

Why has this class been created as Static Nested Class as compared to inner class(non-static nested class) if we wanted to create objects of this class.

ashish2199
  • 393
  • 3
  • 13
  • You seem to be somehow familiar with nested classes so I am not sure what is confusing you about "how are we able to create object of this static class". It is the same way as creating object of any other class which is `new NameOfClass(arguments)`. Depending on execution location we can use `new Outer.Inner(..)` or just `new Inner(..)`. If that is not what you meant then could you clarify your question? – Pshemo Dec 16 '18 at 19:35
  • Possibly related: [Java inner class and static nested class](https://stackoverflow.com/q/70324), https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html – Pshemo Dec 16 '18 at 19:36
  • @Pshemo I am having trouble understanding why we need static class. Also static is something which is not related to instances so how come we are creating instances of class which is static. – ashish2199 Dec 16 '18 at 19:41
  • General answers about "Why Use Nested Classes?" can be found at https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html. I am guessing that for HashMap and Node "It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used." applies since Node is not private so it doesn't really help with encapsulation (it could also be non-public top level class). – Pshemo Dec 16 '18 at 19:52
  • Another reason to place class in another class is access to private members (Nested class can access private members of Outer class and vice-versa). Anyway decision if class should be (a) nested or not is different than decision if it should be (b) static or not. Answering (b) is usually done by deciding if instance of Nested can exist on its own, or must be related to some instance of Outer class (because its work may require accessing data which Outer class instance holds). – Pshemo Dec 16 '18 at 20:05
  • @Pshemo Thanks for your help. – ashish2199 Dec 17 '18 at 17:36

0 Answers0