3

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?

harpun
  • 4,022
  • 1
  • 36
  • 40
Sherwood Lee
  • 237
  • 2
  • 4
  • 12

3 Answers3

10

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.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
2

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>();
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • 2
    In java 7 the `<>` "diamond" generic type-inference operator should avoid the need to specify the generic type, if it can be inferred. His problem is he's using Java 6 (or Java 6 output compatability) on the Linux compiler which fails. – Thomas W Oct 11 '13 at 22:01
  • @ThomasW Well first of all, we don't know that he is using Java 7, and if he isn't then he would have to infer it. I don't understand the downvote. If he is going from Java 7 to Java 6, then he would have to specify the type... I don't think it should be a downvote. – BlackHatSamurai Oct 11 '13 at 22:26
  • I took it, from a Java 7-specific syntax, he was using Java 7 (on the box that worked). See http://docs.oracle.com/javase/tutorial/java/generics/types.html#diamond and http://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7 for a discussion of this new syntax. – Thomas W Oct 11 '13 at 22:31
  • Blaine: reactions like yours here are why so many people never bother leaving comments with down votes. /cc @ThomasW – Andrew Barber Oct 12 '13 at 01:06
  • Sorry for the confusion. In the beginning, I am not sure the version between linux and my computer is different. So I didn't imply that. Anyway, thank you all for answering this!! – Sherwood Lee Oct 12 '13 at 06:12
0

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>();
  }
}
Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
  • Not relevant to his problem, incorrect in regards to the `<>` new Java 7 diamond syntax. I would agree with initializing in the declaration or constructor, though. – Thomas W Oct 11 '13 at 22:03