Skip to main content

HashMap in java ?

5 replies [Last post]
kbanerjee2002
Offline
Joined: 2006-07-18

import java.util.*;
public class NumberNames {
public NumberNames() {
private HashMap map = new HashMap();
public void put(String name, int value){ map.put(chuchu, 9); }
public Set getNames() {
return map.keySet();
}
}
public static void main(String[] args)
{
NumberNames n = new NumberNames();
}
}
output :
D:\>javac NumberNames.java
NumberNames.java:4: illegal start of expression
private HashMap map = new HashMap();
^
NumberNames.java:5: illegal start of expression
public void put(String name, int value){ map.put(name,value); }
^
NumberNames.java:9: ';' expected
}
^
3 errors

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
leeban99
Offline
Joined: 2005-12-23

It seems that u have used access specifier private and public in your constructor whereas variables and objects in construcctor has constructor as their scope if defined in constructor.

deltageek
Offline
Joined: 2007-11-15

You've defined a class member inside of your constructor. This is illegal.
Also, your put() method seems to be using a variable that doesn't exist.

inathaniel
Offline
Joined: 2006-08-26

please go here for your answer

inathaniel
Offline
Joined: 2006-08-26
webkrishna
Offline
Joined: 2007-11-12

That URL is not working now.
As far as i know, we never write methods in the Constructor of the class.

private HashMap map = new HashMap(); - JDK1.5

map.put(chuchu, 9) - needs to String Object and Integer Object, but these are primitive data types.

I have never seen method declaration in the constructor. I don't know whether its possible in JDK1.5 or not.

Regards,
Krishna