why is this syntax not legal..? Can you throw some light on what is the design issue in not allowing HashMap to be static with declarations like this.?
static HashMap<String, String> map2 = new HashMap<String, String> ();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
is perfectly valid provided you have this code at the class level and not in a method.
Where do you have this?
After your update:
You have the variable declared at the method level, static variables are not allowed at the method level.
staticvariables are class variables. One per class. So it does not make sense to have variables that are visible only inside a method, that is going to die after the method is done, to be ‘class level’. At least thats how Java sees it.