<identifier> expected error

Discussion in 'Java' started by Conor, Oct 3, 2006.

  1. Conor

    Conor Guest

    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
     
    Conor, Oct 3, 2006
    #1
    1. Advertising

  2. Conor <> wrote:

    > 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.

    > }


    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Oct 3, 2006
    #2
    1. Advertising

  3. Conor

    Oliver Wong Guest

    "Christopher Benson-Manica" <> wrote in message
    news:efttce$klo$...
    > Conor <> wrote:
    >
    >> 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.
    >


    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
     
    Oliver Wong, Oct 4, 2006
    #3
  4. "Oliver Wong" <> writes:

    > 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.)
     
    Tor Iver Wilhelmsen, Oct 4, 2006
    #4
  5. Conor

    Oliver Wong Guest

    "Tor Iver Wilhelmsen" <> wrote in message
    news:...
    > "Oliver Wong" <> writes:
    >
    >> 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.)


    You're correct, thanks.

    - Oliver
     
    Oliver Wong, Oct 4, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. barry

    expected identifier error

    barry, Dec 24, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    5,818
    barry
    Dec 25, 2005
  2. Kobu
    Replies:
    7
    Views:
    483
  3. Replies:
    2
    Views:
    1,825
  4. Replies:
    2
    Views:
    936
    Owen Jacobson
    Dec 11, 2007
  5. S_K
    Replies:
    0
    Views:
    684
Loading...

Share This Page