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 error question: toString() in java.lang.Object is not defined in a public class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "error question: toString() in java.lang.Object is not defined in a public class " Watch "error question: toString() in java.lang.Object is not defined in a public class " New topic
Author

error question: toString() in java.lang.Object is not defined in a public class

Ernesto Leyva
Ranch Hand

Joined: Feb 23, 2006
Posts: 62
Hello,

Someone knows why the java compiler throws the following error:

"toString() in java.lang.Object is not defined in a public class or interface; cannot be accessed from outside package"

I mean toString() is a public method from object so is there by default.

I am running JSDK 1.4.2 on windows xp



import com.company.*; => this is the import clause at the top of the program. The program lives in the default package.



String response = XRunner.getResult().toString(); ==> This is the line of the program that throws the compilation error.


this is the code in the package:

package com.company; ==>package declaration


public final class XRunner{ ==>public class so can be referenced


public static final XResult getResult(){ ==>Static method

}

}

interface XResult { ==> Result interface default access
public Object getValue();
}

Note that if I change the error line like follows:

String response = XRunner.getResult() + "";

The compiler does not complain and the program runs as expected.
I mean if the problem is interface XResult is not public why the second form compiles?

any ideas?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
It is not the XRunner class which is not public, it is the XResult class. You need to return a class which implements XResult, and that has to have public access, or must be in the same package.
Konstantin Chikarev
Greenhorn

Joined: Feb 19, 2005
Posts: 26
Just create method toString() in XResult


http://antilogics.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
That's not, I believe, the problem. All classes have an implicit public String toString() method, inherited from java.lang.Object. I think the problem is that the class XResult or whatever doesn't have public access, so it is the class whihc is inaccessible, not the method.
Rodrigo Lopes
Ranch Hand

Joined: Feb 29, 2008
Posts: 119
You said that the code compiles, but does it run without errors when you use the second option?
String response = XRunner.getResult() + "";
Manish Singh
Ranch Hand

Joined: Jan 26, 2007
Posts: 160
Problem is that
----------------------------------------------------------------
interface XResult { ==> Result interface default access
public Object getValue();
}
---------------------------------------------------
has default access in your code.

So objects of this class is not accessible outside of its package.

-----------------------------
String response = XRunner.getResult().toString();
---------------------------------------
Now you are trying to access "XResult" out side of its package thats why you are getting the compiler error.

To fix this problem.
Declare "XResult" interface in a seperate source file and mark it as public
Ernesto Leyva
Ranch Hand

Joined: Feb 23, 2006
Posts: 62
Hi

if I delcare the interface XResult public like this:

public interface XResult {}

Now this line compiles

response = TVRunner.getResult().toString();

However the question remians.

Suppose the interface is declared in the default package like this:

interface XResult {
public Object getValue();
}

Why the code compiles with this line

response = TVRunner.getResult() + "";
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
the actual code (copy/paste, use CODE tags),
and the actual error,
are always helpful in these types of questions


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Ernesto]: Suppose the interface is declared in the default package like this:

interface XResult {
public Object getValue();
}

Why the code compiles with this line

response = TVRunner.getResult() + "";


Whether this compiles or not will depend on whether or not this code is in the same package as XResult. Since you've declared XResult with package access (no modifier), it's only accessible within the same package. If you're in the same package, the code will work; otherwise, it will not.


"I'm not back." - Bill Harding, Twister
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
If XResult is an interface, there ought to be a class which implements it. That class ought to have public access.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think it's entirely possible to have a public interface with no public implementing class. The interface must be public in order to be useful, but the implementing class may be protected, package-access, or even private. There are many common objects we encounter in Java that may be implemented with non-public classes, such as a Connection or HttpRequest. These are interfaces - they have implementing classes, but we rarely need to use anything but the interface.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
. . . but I thought (maybe mistakenly) from the original post that he had a class implementing XResult in his other package. That would have to be a public class.

But I might have been mistaken about that point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: error question: toString() in java.lang.Object is not defined in a public class