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?

2
  • 3
    This is a Java 1.7 versus 1.6 error -- 1.6 doesn't have the <> "diamond" generic type-inference syntax. Commented Oct 11, 2013 at 22:00
  • I think you are right. That's the version error. Thanks! Commented Oct 11, 2013 at 22:27

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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>();

5 Comments

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.
@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.
I took it, from a Java 7-specific syntax, he was using Java 7 (on the box that worked). See docs.oracle.com/javase/tutorial/java/generics/… and stackoverflow.com/questions/4166966/… for a discussion of this new syntax.
Blaine: reactions like yours here are why so many people never bother leaving comments with down votes. /cc @ThomasW
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!!
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>();
  }
}

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.