File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Why Won't ThisCompile? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Won Watch "Why Won New topic

Why Won't ThisCompile?

Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57
I get a compile error in It doesn't recognize the method MyFunc which it calls. Here's what I have, Both and are in c:\java. After succesfully compiling, I copied MyClass.class to my c;\java\com\util directory, since imports it. I want to call the MyFunc function in from main in Also, it it OK to have a .java class file (such as my that does NOT have a function main in it?
Here's my file:
package com.util;
public class MyClass
void MyFunc()
System.out.println("It printed in MyClass");

Here's my file:

import com.util.*;
public class MyTest
public static void main(String[] args)
MyTest obj = new MyTest();
Bill Tripper

Joined: May 30, 2001
Posts: 24
You've specified class MyClass as belonging to package com.util. Class MyTest has no package specification. That is, these two classes are not in same package.
Within class MyClass, you've defined method MyFunc() as "void MyFunc()". You're not specifying any access modifier, i.e, you're using the "default" modifier. This makes method MyFunc() accessable only within the same package.
Since class MyClass is not in the same package as class MyTest, method MyFunc() is not accessable from within class MyTest. The solution is to define method MyFunc() as "public void MyFunc()".
Also, typically method calls start with a lower case letter. That is, it's more normal to name your method "myFunc" or myMethod" instead of "MyFunc".
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
By importing files it does not neccaserly give you rights to the methods of other objects
myTest does not have a method called myFunc()hencs the compiler error if you were to extend instead of importing it may help
yes you can have java files without a main method
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If you are using a jdk less than 1.3, you may experience another problem, described here in this rather long post:
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059

Notice obj is a new MyClass rather than a new MyTest.
Also myFunc() is public.
main() only has to be in the class that "kicks off" the application.
I also had to replace the * in the import statement with the name of the class (MyClass) before it would compile.
[This message has been edited by Marilyn deQueiroz (edited July 24, 2001).]

"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:
subject: Why Won't ThisCompile?
jQuery in Action, 3rd edition