<identifier> expected error

C

Conor

Hi,

I have the following code which uses hashmaps within hashmaps to store
different views on a set of objects depending on their attributes:

class MCPStore {

HashMap<String, HashMap> categories = new HashMap<String, HashMap>();

HashMap<String, ArrayList> processes = new HashMap<String,
ArrayList>();

categories.put("pname", processes);

....
}

When I compile the above code I get the following error:

$ javac MCPStore.java
MCPStore.java:11: <identifier> expected
categories.put("processes", processes);
^

Can anyone explain why this is, as I'm stumped.

Thanks,

Conor
 
C

Christopher Benson-Manica

Conor said:
class MCPStore {
HashMap<String, HashMap> categories = new HashMap<String, HashMap>();
HashMap<String, ArrayList> processes = new HashMap<String,
ArrayList>();
categories.put("pname", processes);

Perhaps you intended this to be in a function? It can't be by itself,
in any case.
 
O

Oliver Wong

Christopher Benson-Manica said:
Perhaps you intended this to be in a function? It can't be by itself,
in any case.

If this is to be done once when the object is constructed, you could put it
in the constructor (or, more risky, in the static initializer):

class MCPStore {
private final HashMap<String, HashMap> categories;

public MCPStore() {
categories = new HashMap<String, HashMap>();
HashMap<String, ArrayList> processes = new HashMap<String,ArrayList>();
categories.put("pname", processes);
}
}

- Oliver
 
T

Tor Iver Wilhelmsen

Oliver Wong said:
If this is to be done once when the object is constructed, you could put it
in the constructor (or, more risky, in the static initializer):

We,, they are instance fields so they would need to be in an instance
initializer. (Aka. code that gets put into every constructor.)
 
O

Oliver Wong

Tor Iver Wilhelmsen said:
We,, they are instance fields so they would need to be in an instance
initializer. (Aka. code that gets put into every constructor.)

You're correct, thanks.

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top