Forums Register Login

Access a class no package

+Pie Number of slices to send: Send
I have 2 class A and B

class A:
<no package name>
public class A {
public doIt() {}
}

class B:

package com.abc;
public class B {
// A a = new A();
// a.doIt();
}

In class B, I want to access doIt() method of class A.
So, how do I do?

Help me.
+Pie Number of slices to send: Send
either include class A into package say com.xyz and import com.xyz.A in class B's source or remove package declaration from class B's source.
+Pie Number of slices to send: Send
Thank Harshil Mehta

2 ways you mentioned I can do
But, if do not modify the package name, in class B can I not access method in class A?
+Pie Number of slices to send: Send
I think you can. You can keep your class structure, since that looks like a classpath problem.
Creating classes with no packages is not a good thing, though.
+Pie Number of slices to send: Send
It's not possible using Sun's recent Java compilers; you might be able to use an older compiler. In that case you'd need to have

import A;

in class B's source file; this syntax is, strictly, not legal Java, but older compilers accepted it. Putting classes in the default package is strongly discouraged, and here's one good reason for that.
+Pie Number of slices to send: Send
Thank you.

I khow creating classes with no packages is not good thing. But class A is not my class(reuse).

I imported A in class B(import A and compile error. And from your message I know it is not possible using Sun's recent Java compilers.
To reuse A, maybe I must remove package name of B class.

Thank you very much.
I don't like that guy. The tiny ad agrees with me.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 4119 times.
Similar Threads
Checked exception and interface.
Output of the Program
doubt in access modifiers...
Overloaded method question
Calling a Overriding method in a abstract class
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:37:30.