简体   繁体   中英

Weird Compiling/Linking Issue While Adding Objects in an Array

I'm running into a weird compilation issue, and I don't even know if it is a compiling or linking or syntax problem or what. Sorry to be so vague, but I'm struggling to even diagnose this problem.

To explain: I am in the middle of writing a fairly large Java program - for me, anyway. In my code, I set up an array of "widget" objects. The widgets are defined a "widget.java," file; the code which creates and populates the widget array is in another file. Trying to figure this out before I posted anything, I stripped down my code and wrote a little toy version, below:

import java.io.*;
import java.lang.String;
import java.lang.NullPointerException;

public class arrayObjTest {

    public static void main(String[] args) throws IOException {

    widget yellow = new widget("Yellow");

    widget[] myWidgets = new widget[3];

    myWidgets[0] = new widget("Blue");  
    myWidgets[1] = new widget("Red");   
    myWidgets[2] = yellow;  

    }
}

class widget {

    String color;

    public widget(String a) {
      color = a;
    }
}

The toy version (above) compiles just fine. But when I import just this...

129      widget[] myWidgets = new widget[3];
130      
131      myWidgets[0] = new widget("Blue");

...into my big code, I get these compilation errors:

C:\Users\Pete\Desktop\Java Playground\widgetSim>
C:\Users\Pete\Desktop\Java Playground\widgetSim>
C:\Users\Pete\Desktop\Java Playground\widgetSim>"C:\Program Files\Java\jdk1.8.0_20\bin\javac.exe" widgetMachine.java
.\widgetObject.java:131: error: ']' expected
        myWidgets[0] = new widget("Blue");
                  ^
.\widgetObject.java:131: error: ';' expected
        myWidgets[0] = new widget("Blue");
                   ^
.\widgetObject.java:131: error: illegal start of type
        myWidgets[0] = new widget("Blue");
                     ^
.\widgetObject.java:131: error: <identifier> expected
        myWidgets[0] = new widget("Blue");
                      ^
.\widgetObject.java:131: error: ';' expected
        myWidgets[0] = new widget("Blue");
                          ^
.\widgetObject.java:131: error: illegal start of type
        myWidgets[0] = new widget("Blue");
                                 ^
.\widgetObject.java:131: error: <identifier> expected
        myWidgets[0] = new widget("Blue");
                                  ^
.\widgetObject.java:131: error: ';' expected
        myWidgets[0] = new widget("Blue");
                                        ^
.\widgetObject.java:131: error: cannot find symbol
        myWidgets[0] = new widget("Blue");
        ^
  symbol:   class myWidgets
  location: class widgetObject

9 errors

C:\Users\Pete\Desktop\Java Playground\widgetSim>
C:\Users\Pete\Desktop\Java Playground\widgetSim>
C:\Users\Pete\Desktop\Java Playground\widgetSim>

What gives? The compiler doesn't complain when I create an array of widgets in line 129, but when I try to insert a widget, I get the above errors. I've tried a lot of experimenting, but can't figure out what I'm doing wrong.

I don't think its a syntax error because this code works in the toy version. Is this some kind of linking problem? The code creating/populating the widget array can't see "widget.java" or something?

In Java, you can declare an instance field within a class, and you can also initialize it. So when you write

 widget[] myWidgets = new widget[3];

 myWidgets[0] = new widget("Blue");

in the middle of a class, but outside of any method, the first line is allowable because it defines an instance field myWidgets and initializes it.

But the second line isn't of this type. It's just some executable code. That isn't allowed outside of a method.

Specifically, the compiler is complaining about the 0 because it would possibly have been valid if that line had started with

myWidgets[]

and myWidgets had been a class. In other words, it's not till the compiler reaches the 0 that it realises that this cannot possibly be either an instance field or the start of a method signature (it hasn't checked whether myWidgets is a type by that point), so that's where it complains.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM