fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class HashCode
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. HashCode x = new HashCode();
  13. Set<Sample> samples = new HashSet<>();
  14. samples.add(x.new Sample("Goofy"));
  15. samples.add(x.new Sample("Donald"));
  16. System.out.println(samples.size()); //Output is 2
  17. }
  18.  
  19. private class Sample {
  20. private String name;
  21.  
  22. public Sample(String name) {
  23. this.name = name;
  24. }
  25.  
  26. public int hashCode() {
  27. return 1;
  28. }
  29.  
  30. public boolean equals(Object o) {
  31. return name.equals(((Sample)o).name);
  32. }
  33. }
  34. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:13: error: non-static variable this cannot be referenced from a static context
		samples.add(new HashCode.Sample("Goofy"));
		            ^
Main.java:14: error: non-static variable this cannot be referenced from a static context
		samples.add(new HashCode.Sample("Donald"));
		            ^
2 errors
stdout
Standard output is empty