java - Error extending LinkedHashMap -


i trying extend linkedhashmap students class. want bring in functionalities of map map.put(value), map.get(key). create object inside psvm , not making static references still below error. can point me mistake committting here? there better approach achieve task? in advance!

import java.util.linkedhashmap;  public class students<integer,string> extends linkedhashmap<integer,string> {      public static void main(string args[]) {                     // line 5         students<integer,string> students = new students<>();    // line 6         students.put(1, "s1");         students.put(2, "s2");         students.put(3, "s3");          system.out.println(students.get(1));     }  } 

error message:

>> javac students.java  students.java:5: error: non-static type variable string cannot referenced static context         public static void main(string args[]) {                                 ^ students.java:6: error: non-static type variable integer cannot referenced static context                 students<integer,string> students = new students<>();                          ^ students.java:6: error: non-static type variable string cannot referenced static context                 students<integer,string> students = new students<>();                                  ^ students.java:6: error: unexpected type                 students<integer,string> students = new students<>();                                                                 ^   required: class   found:    <integer,string>students<integer,string>   integer,string type-variables:     integer extends object declared in class students     string extends object declared in class students 4 errors 

by doing

class student<integer, string> 

you have defined own generic types integer , string. these associated instance of class student attempting use these new generic types in main() method static rather instance class , not allowed. intended use java.lang.string

the simple solution is

  • don't define own generic types, don't need to.
  • don't extend linkedhashmap rather use composition limits methods expose.

e.g.

public class students {     private final map<integer, string> map = new linkedhashmap();      public void put(int num, string str) { map.put(num, str); }      public string get(int num) { return map.get(num); }      public static void main(string args[]) {          students students = new students();          students.put(1, "s1");         students.put(2, "s2");         students.put(3, "s3");          system.out.println(students.get(1));     } } 

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -