File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes I Still Get the Same Compile Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I Still Get the Same Compile Error" Watch "I Still Get the Same Compile Error" New topic
Author

I Still Get the Same Compile Error

Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57

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!
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4698
    
    7

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).]


Junilu - [How to Ask Questions] [How to Answer Questions]
Jill Rains
Greenhorn

Joined: Apr 16, 2001
Posts: 4
If you declared in class MyTest:
MyClass obj = new MyClass();
obj.MyFunc();
and also declared MyFunc to be public void MyFunc() it would work.
Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57

[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 ]
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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)
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10
I'm closing this thread. Please post to the original thread.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I Still Get the Same Compile Error