Home arrow static arrow Java Programming [Archive] - Accessing other classes in the same package
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Accessing other classes in the same package
This topic has 7 replies on 1 page.

Posts:15
Registered: 7/10/04
Accessing other classes in the same package  
Jul 10, 2004 6:02 AM



 
I have the following two classes in the same package. Why cant I access the myString vairable from Test2 like I try and do below?

package test;

class Test1 {
public static String myString = "Hello world";
}

package test;

class Test2 {
static void printMe(){
System.out.println(Test1.myString);
}

public static void main(String args[]) {
printMe();
}
}
 

Posts:24,036
Registered: 2/3/03
Re: Accessing other classes in the same package  
Jul 10, 2004 7:36 AM (reply 1 of 7)



 
You can. Delete both class files, make sure you have the appropriate package statements in both source files, and recompile. Let us know the EXACT error you get if you try this and it doesn't work.
 

Posts:15
Registered: 7/10/04
Re: Accessing other classes in the same package  
Jul 10, 2004 7:49 AM (reply 2 of 7)



 
I deleted both class files and recompiled Test1.java. It compiles fine and the recompile Test2.java and get the following error:

C:\Code\test\Test2.java:5: cannot resolve symbol
symbol : variable Test1
location: class test.Test2
System.out.println(Test1.myString);
^
1 error

Tool completed with exit code 1
 

Posts:24,036
Registered: 2/3/03
Re: Accessing other classes in the same package  
Jul 10, 2004 7:51 AM (reply 3 of 7)



 
I deleted both class files and recompiled Test1.java.

Did you verify that the first line in Test2.java is "package test;" ?
 

Posts:15
Registered: 7/10/04
Re: Accessing other classes in the same package  
Jul 10, 2004 7:57 AM (reply 4 of 7)



 
Yes I did. If you look at the code above it is there aswell. Both these files are in the same directory called test.
 

Posts:11,200
Registered: 7/22/99
Re: Accessing other classes in the same package  
Jul 10, 2004 9:18 AM (reply 5 of 7)



 
Have you modified the class search path in some way?

Try compiling with:
javac -classpath . test/Test2.java

And running with:
java -classpath . test/Test2

Note that these commands should be typed from the directory above "test", that is the directory where test is located.
 

Posts:15
Registered: 7/10/04
Re: Accessing other classes in the same package  
Jul 10, 2004 9:52 PM (reply 6 of 7)



 
Thanks this solved it.
Why when I take the "package test;" line of code away I can compile it normally?
I was working on a project in netbeans when this problem came up but when I fixed some other errors in my code this problem just dissapeared.
Well thanks again for the help.
 

Posts:11,200
Registered: 7/22/99
Re: Accessing other classes in the same package  
Jul 10, 2004 10:40 PM (reply 7 of 7)



 
Why when I take the "package test;" line of code away I can compile it normally?

This depends on how you call the compiler and how you have set the class search path. Classes that are in a package are expected to be found from some place in the search path under a directory that has the same name as the package. The default class path consists of only the current directory, ".", so you can avoid all problems by compiling and running from the directory above "test" rather than "test" itself.
 
This topic has 7 replies on 1 page.