• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why Won't ThisCompile?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get a compile error in MyTest.java. It doesn't recognize the method MyFunc which it calls. Here's what I have, Both MyTest.java and MyClas.java are in c:\java. After succesfully compiling MyClass.java, I copied MyClass.class to my c;\java\com\util directory, since MyTest.java imports it. I want to call the MyFunc function in MyClass.java from main in MyTest.java. Also, it it OK to have a .java class file (such as my MyClass.java) that does NOT have a function main in it?
Here's my MyClass.java file:
package com.util;
public class MyClass
{
void MyFunc()
{
System.out.println("It printed in MyClass");
}
}

Here's my MyTest.java file:

import com.util.*;
public class MyTest
{
public static void main(String[] args)
{
MyTest obj = new MyTest();
obj.MyFunc();
}
}
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using a jdk less than 1.3, you may experience another problem, described here in this rather long post: http://www.javaranch.com/ubb/Forum1/HTML/001525.html
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic