• 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

I Still Get the Same Compile Error

 
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 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!
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[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 ]
 
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
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)
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm closing this thread. Please post to the original thread.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic