I posted to Java in General (beginner). My post was today's and entitled "Why Won't This Compile?".
I received a few replies but none that solve my problem. Both MyTest.java and MyClass.java are in my c:\java directory from where I compile them. I want MyTest.java and MyClass.java to be in different packages. That's why I have a package statement at the top of MyClass.java. I want to call method MyFunc (in MyClass.java) from method main in MyTest.java.
By importing com.util.*; (with MyClass.class in c:\java\com\util) shouldn't MyTest.java recognize MyFunc? After successfully compiling MyClass.java, I copied MyClass.class to c:\java\com\util. Isn't this required to make my import statement work, so MyTest.java can access the byte of MyFunc from c:\java\com\util?
I'm sure the solution is easy. But for a beginner like me it's not. Thanks!
Gary, The replies to the other posts already gave you the answer you needed. To make it clear: 1. MyClass is declared to belong to the com.util package and MyClass.MyFunc() has default(package) visibility. 2. Since MyFunc() has default(package) visibility, only classes that belong to the com.util package can access MyFunc(). 3. Since you do not have a package declaration in the MyTest.java, the MyTest class will belong to the default package (not com.util even if you copy the MyTest.class to c:\java\com\util). 4. Even if you import com.util.* in MyTest.java, you will still not have access to the MyFunc() method because MyTest does not belong to the same package as MyClass.
Solutions (any one will do): A. put a "package com.util;" declaration in MyTest.java B. make MyFunc() a public method C. make MyFunc() a protected method and make MyTest extend MyClass. Also, you can save yourself the trouble of having to copy MyClass.class to c:\java\com\util by compiling with this: javac -d c:\java\com\util MyClass.java [This message has been edited by JUNILU LACAR (edited July 24, 2001).]
If you declared in class MyTest: MyClass obj = new MyClass(); obj.MyFunc(); and also declared MyFunc to be public void MyFunc() it would work.
Joined: Jun 24, 2000
[I tried this and I still get the unregognized MyFunc compile error in MyTest.java]Originally posted by Jill Rains: If you declared in class MyTest: MyClass obj = new MyClass(); obj.MyFunc(); and also declared MyFunc to be public void MyFunc() it would work.[/I trioed this and I still get the unrecognized MyFunc compile error in MyTest.java ]
please see my reply to your first post... it might have something to do with it.
A quick way to determine if this is the problem: Move the *source* file (the MyClass.java file) to some other directory. It doesn't matter which one, as long as it's not the same as MyTest.java. If MyTest.java compiles ok, then you will need to learn and use the -d switch (or use an IDE)