So I have this class (let's call it "A") with this get()
method which returns a class field, and which I want to access from another class ("B"). Important: both classes are abstract
. These classes are in the same package
(package private
for all A
and B
members). The Main
class has no package (or better, the default one).
[before: twisted and useless description]
I need from B
class to access A
's get()
method.
Tried with:
1) non-static call Aobj.get()
starting from the main
-located root object, but it seems to want the static call (weird, none of the members is static
);
2) tried to access it statically and I got the Cannot make a static reference to the non-static field
error (of course).
...so I'm stucked, still getting the accessing syntax error, both ways I try. Any suggests?
EDIT :
So I came to an end. I'll now try to explain my errors.
Basically, the problem was I wanted to access an object located in the Main
class (default package) from a class within a package. So I got a visibility problem.
In other circumstances, I would have resolved importing the class, but I figured out that you cannot import a class located in the default package, so I created a main
package and imported Main
class in B
class.
The worst mistake I kept making was thinking I had to access the main.main(String[])... etc ...Aobj
starting from the main
itself, which is a really really bad thought (still can't figure out how I thought it).
In fact, like Stephen C pointed out:
The is true irrespective of the situation with packages, and irrespective of the path you take through other classes.
Finally, the code example... to be honest, the code is now really different from the days I wrote this question, and honestly I can't reproduce the faulty conditions since I don't remember all the faulty logic I was trying to achieve. (My fault.) I only wish to have explained myself properly this time.
Thanks to everyone to tried to help.
If B's get()
method is not static
then you need an instance of B
if you want to call the method; e.g.
B b = ....
b.get();
The is true irrespective of the situation with packages, and irrespective of the path you take through other classes, etcetera. (Though I'm not sure I entirely understand your "prose" description of all of that ...)