tags:

views:

110

answers:

4

I write:

public interface Map <Key, Value> cells;

eclipse complains and says "{ expected".

public interface Map <Key, Value> cells{};

I then write the above in eclipse and it complains "Syntax error on token "cells", delete this token".

What on earth should i do? Google is not my friend here as i can't find code snippets to work out the syntax of this.

+1  A: 

What are you trying to do ? Define a new type or define a member of type Map?

If you are defining a member, you don't need the interface keyword before it. You only need interface if you are defining a new type.

Uri
+1  A: 

You want:

public Map<Key,Value> cells;

To create a variable called cells of type Map.

public interface Map<Key,Value> cells;

Is declaring (incorrectly) a user-defined interface called Map - probably not what you are trying to do.

Strawberry
+4  A: 

Are you trying to declare a member variable named cells? If so, the way to do it is:

public class Foo {

   // ...

   public Map<Type1, Type2> cells;

   // ...

}

If you are trying to declare a variable within a method, do this:

public class Foo {

    // ...

    public void myMethod() {
        // ...

        Map<Type1, Type2> cells;

        // ...
    }

    // ...
}

Edit: It looks like you are confused by Map <Key, Value>, so I'll try to explain.

Key is a placeholder for the data type that the Map object will use for its set of keys. Similarly, Value is a placeholder for the data type that it will use for its set of values. A valid combination would be, for example, Map <String, Integer>. This means that the Map object will map a set of String objects to a set of Integer objects.

William Brendel
+4  A: 

You probably mean

public Map<Key, Value> cells;

The interface keyword is used to define an interface. implements is used when declaring a class that implements a interface.

Johannes Weiß
Dang, It's still not working.Error:"Key and Value cannot be resolved to a type.".
Randle
That is another error. Key and Value have to be Java classes. A working sample is `public Map<String, Integer> cells;`. I though you just used Key and Value as a placeholder.
Johannes Weiß
@Randle: look at the comment William Brendel made above. You need to replace "Key" and "Value" with your actual key and value types.
Laurence Gonsalves
Cheers Laurence!
Randle