2

I learnt that static class is a class whose members MUST be accessed without an instance of a class.

In the below code from java.util.HashMap, jdk 1.8,

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {

        .........

        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;
             }
             ........
         }
         ..........
}

What is the java syntax to invoke a constructor

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

of a nested static class Node?

overexchange
  • 15,768
  • 30
  • 152
  • 347

3 Answers3

2

I learnt that static class is a class whose members MUST be accessed without an instance of a class.

More correctly, a static nested class is a class whose instances are instantiated without any reference to an instance of the enclosing class.

A static nested class is regarded as a member of the enclosing class (along with its methods and fields). However, in every way that matters, a static nested class functions just like a top level class.

To create an instance of a static nested class, you use this syntax:

EnclosingClass.MemberClass myInstance = new EnclosingClass.MemberClass();
scottb
  • 9,908
  • 3
  • 40
  • 56
  • error: MemberClass is not public in EnclosingClass; cannot be accessed from outside package – Anonymous Coward Sep 03 '15 at 04:56
  • Nested static classes, unlike top level classes (which may only be public or default access), may have any of the access modifiers that are allowed for any member of an enclosing class (i.e., public, private, default, protected). To make a nested static class accessible outside a package, simply make it and its enclosing class public. – scottb Sep 03 '15 at 04:58
2

I don't think you can "see" that static inner class; it is package protected and you generally don't see people trying to jack their stuff into any of the java.* packages.

This compiles, but yuk. I didn't even know you could hijack the java.* packages in this manner.

package java.util

import java.util.HashMap;
import java.util.Map;

public class InstantiateNode {

  public static void main(String[] args) {
    HashMap.Node<String,String> mapNode = new HashMap.Node<String, String>(1, "hey", "you", null);
  }
}
Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
  • yuk indeed, don't do that. But this actually manages to do what the question requests. – Anonymous Coward Sep 03 '15 at 05:37
  • 1
    You can't hijack the `java.*` packages for security reasons. If you try you will get a `java.lang.SecurityException: Prohibited package name: java.util`. – René Link Sep 03 '15 at 05:44
1

None, there is no sintax for that.

Node has package access. This means you cannot access from code outside of the package where HashMap belongs to.

In the unlikely case that you are writing code inside that package the sintax would be :

HashMap.Node<KeyType, ValueType> node = 
   new HashMap.Node<>(hash, key, value, nextNode );
Anonymous Coward
  • 3,140
  • 22
  • 39