Home arrow static arrow Java Programming [Archive] - weird Can not access class from outside package problem
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - weird Can not access class from outside package problem
This topic has 5 replies on 1 page.

Posts:82
Registered: 5/23/03
weird Can not access class from outside package problem  
Jul 31, 2004 8:32 PM



 
Hi, I have this problem and it seems the error message is very clear what the problem is but I did make the GeneralTestResult class PUBLIC already. What could be wrong? I did search for it but this is too general and didn't find any solution. Anyone can point out the problem? TIA.

public abstract class GeneralTestResult{  public static int FAILING_THRESHOLE = 25;    public abstract String getTestName();  public abstract boolean isPassed();  public abstract int getLeftEyeResult();  public abstract int getRightEyeResult();  public abstract Calendar getTestDate();} // in a different file package VisionSaver.Tests.TestResults; import java.util.*; public class AcuityORContrastTestResult extends GeneralTestResult{  private String m_testName;  private int m_leftEyeVision, m_rightEyeVision;  private Calendar m_testDate;...} // in a different filepackage VisionSaver.Tests; import VisionSaver.Tests.TestResults.*; public class Acuity extends VirtualKeyboard implements ActionListener, Runnable, WindowListener{ ...AcuityORContrastTestResult r = new AcuityORContrastTestResult(...);...}


The compiling error is:
"Acuity.java": GeneralTestResult() in VisionSaver.Tests.TestResults.GeneralTestResult is not defined in a public class or interface; cannot be accessed from outside package at line 529, column 40
 

Posts:37,103
Registered: 3/30/99
Re: weird Can not access class from outside package problem  
Jul 31, 2004 11:59 PM (reply 1 of 5)



 
It appears that GeneralTestResult is in a different package (i.e. none) than the class that's trying to use it. Either they have to be in the same package, or you have to import it. I don't think a class that's in a package can access one that's not. You should put all your classes into packages, except for quick & dirty test classes.

Also, it's convention to make package names all lowercase and class names camel case--first letter of each word capitalized.
 

Posts:11,200
Registered: 7/22/99
Re: weird Can not access class from outside package problem  
Aug 1, 2004 12:04 AM (reply 2 of 5)



 
Because of a warp in the language specification, classes in an unnamed package cannot be accessed from classes in a named package. The only solution is to move GeneralTestResult to a named package.
 

Posts:82
Registered: 5/23/03
Re: weird Can not access class from outside package problem  
Aug 3, 2004 10:37 PM (reply 3 of 5)



 
The GeneralTestResult class is a packaged class. Sorry, when I cut and paste, I didn't copy everything. So here is the complete GeneralTestResult file:

package VisionSaver.Tests.TestResults; import java.util.*; public abstract class GeneralTestResult{  public static int FAILING_THRESHOLE = 25;   public abstract String getTestName();  public abstract boolean isPassed();  public abstract int getLeftEyeResult();  public abstract int getRightEyeResult();  public abstract Calendar getTestDate();}


From the compiler's message, it seems like the import is OK. It just won't allow me to access non-public class but my classes here are declared public. Any other ideas? TIA
 

Posts:11,200
Registered: 7/22/99
Re: weird Can not access class from outside package problem  
Aug 4, 2004 12:39 AM (reply 4 of 5)



 
a more careful look at the error message reveals that it is not complaining about the class, but the default constructor GeneralTestResult(). But because the class is declared public the default constructor should be public as well. Either you have two classes called GeneralTestResult and the other is getting used before this one, or you have found a bug in your compiler.

What compiler do you use? "javac" doesn't give messages like the one you posted.
 

Posts:82
Registered: 5/23/03
Re: weird Can not access class from outside package problem  
Aug 4, 2004 1:11 AM (reply 5 of 5)



 
I found the problem. It's not the compiler's bug or anything. The AcuityORContrastTestResult constructor doesn't take no argument. Usually, I just make a call and try to compile and if it complains about "can't resolve constructor blah blah blah then I know the call is valid but missing arguments. This time, it complains about that but also something else (quoted earlier). So I thought there is another problem beside the fact that I'm not calling the constructor correctly. After giving the correct argument, it compiles OK.

Thanks you guys all for your help.
 
This topic has 5 replies on 1 page.