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?

1
  • Your most recent edit makes your question incorrect. "Static" and "inner" are mutually exclusive; "static nested class" was the appropriate term. Commented Sep 3, 2015 at 5:32

3 Answers 3

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();
Sign up to request clarification or add additional context in comments.

2 Comments

error: MemberClass is not public in EnclosingClass; cannot be accessed from outside package
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.
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);
  }
}

2 Comments

yuk indeed, don't do that. But this actually manages to do what the question requests.
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.
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 );

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.