-1

I am struggling to understand the workflow when you have multiple Java files.

myNode.java:

package x;
class myNode {
    private myNode next = null;
    private int d;
    myNode(int d) {
        this.d = d;
    }
    void append(int d) {
        myNode curr = this;
        while (curr.next != null) {
            curr = curr.next;
        }
        curr.next = new myNode(d);
    }
}

myMain.java:

package x;
class myMain {
    public static void main() {
        myNode x = new myNode(1);
        x.append(2);
    }
}

I get the following error message when I try to compile myMain.java

error: cannot find symbol
        myNode x = new myNode(1);
        ^
  symbol:   class myNode
  location: class myMain
JeanieJ
  • 328
  • 3
  • 10

1 Answers1

2

You have been told that java classes can be accessed from other java classes without having to explicitly import them if they are in the same package. That's true. But that's not the whole story.

javac will not try to access a java file that you did not explicitly tell it to access. So, when you try to compile with javac you have to specify in the command line all of the files to be compiled, like this: javac MyClass.java AnotherClass.java.

Also, please note that the convention in java is to use capital first letter for class names.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
  • Thanks! I've only been running javac myClass.java. I tried running the command in the format you suggested, and it was giving the same error. And then I changed my class names to begin with a capital letter and that did the magic! – JeanieJ Sep 10 '16 at 22:18