Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiating inner class

I working on a sample problem of over-ridding hashCode and equals method but getting an error: "No enclosing instance of type CustomHashCodeExample is accessible. Must qualify the allocation with an enclosing instance of type CustomHashCodeExample (e.g. x.new A() where x is an instance of CustomHashCodeExample)." I wrote an inner class HashPerson and I am getting this error when I am trying to instantiate this inner class in another method called testHashCodeOverride().

public static void testHashCodeOverride(){        System.out.println("\nTest HashCode Override Method");     System.out.println("==================================\n");      HashPerson william = new HashPerson("willy");     HashPerson bill = new HashPerson("willy");           } 

This code works fine, even though I dont see static inner class or instantiation of outer class, confused :(

public class HashCodeExample {      public static void testHashCodeOverride() {          HashPerson william = new HashPerson("Willy");         HashPerson bill = new HashPerson("Willy");         System.out.println("Hash code for william  = " + william.hashCode());         System.out.println("Hash code for bill     = " + bill.hashCode());          HashMap table = new HashMap();         table.put(william, "Silly");          if (table.containsKey(william)) {             System.out.println(table.get(william));         } else {             System.out.println("Key " + william + " not found");         }          if (table.containsKey(bill)) {             System.out.println(table.get(bill));         } else {             System.out.println("Key " + bill + " not found");         }       }      class HashPerson {         private static final int HASH_PRIME = 1000003;          public HashPerson(String name) {             this.name = name;         }          public String toString() {             return name;         }          public boolean equals(Object rhs) {             if (this == rhs)                 return true;              // make sure they are the same class             if (rhs == null || rhs.getClass() != getClass())                 return false;              // ok, they are the same class. Cast rhs to HashPerson             HashPerson other = (HashPerson) rhs;              // our test for equality simply checks the name field             if (!name.equals(other.name)) {                 return false;             }              // if we get this far, they are equal             return true;         }         public int hashCode() {             int result = 0;             result = HASH_PRIME * result + name.hashCode();             return result;         }         private String name;      } } 
like image 528
t0mcat Avatar asked Nov 01 '10 16:11

t0mcat


People also ask

Can we instantiate inner class in Java?

If the inner class is static, then the static inner class can be instantiated without an outer class instance. Otherwise, the inner class object must be associated with an instance of the outer class. The outer class can call even the private methods of the inner class.

How do you instantiate a static inner class?

We can instantiate a static inner class with reflection using InnerClass. class. newInstance(). If we need an instance of the outer class to instantiate a non-static inner class, we can specify it before a new operator.

What is an inner class in Java?

Java inner class or nested class is a class that is declared inside the class or interface. We use inner classes to logically group classes and interfaces in one place to be more readable and maintainable. Additionally, it can access all the members of the outer class, including private data members and methods.

How do you instantiate a class in Java?

Instantiating a ClassThe new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The new operator returns a reference to the object it created.


2 Answers

I think you want to declare the HashPerson class as static. Otherwise it can only be instantiated in the context of the containing class, either in a method of the containing class or using code like this:

ContainingClass container = new ContainingClass(); HashPerson william = container.new HashPerson("willy"); 

Actually, my rule-of-thumb is to make any nested class static, unless I have a special reason not to. This is also more efficient, because non-static nested classes (called inner classes) always contain an implicit reference to the containing object.

like image 150
MForster Avatar answered Sep 21 '22 21:09

MForster


You need to either make your inner class static, or refer to it through an instance of the outer class. Most likely you just want to make your inner class static.

Non-static members of a class (variables, methods, inner classes) are per instance of the class. Therefore, when accessing non-static members from a static context (such as a static method like testHashCodeOverride), you need to specify an instance of the enclosing class.

like image 40
oksayt Avatar answered Sep 19 '22 21:09

oksayt