Reputation: 237
I have a problem with initializing ArrayLists.
When I used NetBeans 7.3 I tried to do this:
protected Stack<Scope> scopeStack;
protected ArrayList<Scope> allScopes;
scopeStack = new Stack<>();
allScopes = new ArrayList<>();
the file is perfectly compiled and goes fine.
But when I switch to linux using command line to compile java. It gives me an error
src/SymbolTable.java:28: illegal start of type scopeStack = new Stack<>();
SymbololTable.java:29: illegal start of type allScopes = new ArrayList<>();
Is this cause by different versions of java compiler? Or what's the reason that cause this?
Upvotes: 3
Views: 10793
Reputation: 23483
You need to define the type when you initialize if you are using Java 6, like so:
scopeStack = new Stack<Scope>();
allScopes = new ArrayList<Scope>();
Upvotes: 2
Reputation: 61148
I would conjecture that in Netbeans you are using Java 1.7 and on Linux you are using Java 1.6.
The "diamond operator" was only introduced in Java 7.
Use javac -version
to see what version of the compiler your a running.
Upvotes: 10
Reputation: 26502
You should specify the type of your collection in your new
call, and initialize the fields in the proper place. Try either:
Initializing the fields inline
protected Stack<Scope> scopeStack = new Stack<Scope>();
protected ArrayList<Scope> allScopes = new ArrayList<Scope>();
Initializing the fields in a constructor
public class MyClass {
protected Stack<Scope> scopeStack;
protected ArrayList<Scope> allScopes;
public MyClass() {
scopeStack = new Stack<Scope>();
allScopes = new ArrayList<Scope>();
}
}
Upvotes: 0