J
Javabean
Hi all,
I'm trying to understand the concept of packaging which seems relatively
simple and clean, following the directory concept. To test this I've written
two files: 1 which defines a class and puts it inside a package and 1 which
tries to import the class defined in the package.
This is the code for MyClass.java:
package mypackage;
public class MyClass {
public void greeting(){
System.out.println("Hello!");
}
}
I have saved this file to c:\java\mylib\mypackage. The CLASSPATH variable is
set as follows : CLASSPATH = .;c:\java\mylib
And the code for Exercise2.java (which tries to import the MyClass file):
import mypackage.*;
public class Exercise2 {
public static void main(String[] args){
MyClass m = new MyClass();
m.showMe();
}
}
The MyClass class seems to get imported, however I get an error message when
trying to call the showMe() method.
C:\java\test\Exercise2.java:6: cannot find symbol
symbol : method greeting()
location: class mypackage.MyClass
m.greeting();
^
1 error
Anyone have any idea why this is? The greeting() method is declared as
public inside MyClass so I should have access to it... I also tried defining
a no-arg constructor for MyClass which produces a text message but the text
message was never printed as well (even though the object seems to be
created without errors?!)
Thanks for any input.
I'm trying to understand the concept of packaging which seems relatively
simple and clean, following the directory concept. To test this I've written
two files: 1 which defines a class and puts it inside a package and 1 which
tries to import the class defined in the package.
This is the code for MyClass.java:
package mypackage;
public class MyClass {
public void greeting(){
System.out.println("Hello!");
}
}
I have saved this file to c:\java\mylib\mypackage. The CLASSPATH variable is
set as follows : CLASSPATH = .;c:\java\mylib
And the code for Exercise2.java (which tries to import the MyClass file):
import mypackage.*;
public class Exercise2 {
public static void main(String[] args){
MyClass m = new MyClass();
m.showMe();
}
}
The MyClass class seems to get imported, however I get an error message when
trying to call the showMe() method.
C:\java\test\Exercise2.java:6: cannot find symbol
symbol : method greeting()
location: class mypackage.MyClass
m.greeting();
^
1 error
Anyone have any idea why this is? The greeting() method is declared as
public inside MyClass so I should have access to it... I also tried defining
a no-arg constructor for MyClass which produces a text message but the text
message was never printed as well (even though the object seems to be
created without errors?!)
Thanks for any input.