Question
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.
Solution
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.
OTHER TIPS
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
.
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.
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.