I defined 2 java files: Phone2.java and TestPhone2.java. Phone2.java compiles fine and is placed in a package named classes. TestPhone2 is supposed to create an instance of Phone2 but I cannot get TestPhone2.java to compile. Below is my directory structure:
The 2 java files are located here --
Phone2.class is located inside package classes
--
My classpath
is set to c:\myJavaProject.
The java files contain some simple codes from Mala Gupta to test instance and local variables. Here are the codes:
package classes;
class Phone2 {
String phoneNumber = "123456789";
void setNumber () {
String phoneNumber;
phoneNumber = "987654321";
}
}
package classes;
class TestPhone2 {
public static void main (String[] args) {
Phone2 p1 = new Phone2();
p1.setNumber();
System.out.println(p1.phoneNumber);
}
}
It is not clear to me why TestPhone2.java failed to compile. I reviewed some stackoverflow posts on this type of error (here and here) but the posts seem not to be directly on point. One of the posts dealt with static
method and the other dealt with error stemming from an import
or package
statement. I don't think there is an error in my package statement because I used a similar package statement earlier today in the post here and there was no problem.
Below is my exact keystrokes.
Thanks for your feedback.
You have a package. So you must specify the classpath. One way to do that is to use the folder that contains your top level package (
c:\myJavaProject\tint31\
) likeor you might use
-classpath
like