简体   繁体   中英

How to access non-static class fields in a package from the (default package)?

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; eg

  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 ...)

Just from what you've described, (without code), you need access to an instance of b . There are a couple of ways this could go. If the method in A in question could use a different B instance for each call, just add a B argument to that method:

public class B {

    public int get() { //Or whatever the return type of get is
        //...
    }

}

public class A {

    public int methodThatNeedsABInstance(B instance) {
        int x = instance.get(); //Can call get method
    }

}

Which is then called by:

B b = new B();
A a = new A();
a.methodThatNeedsABInstance(b);

If each A instance should use the same B instance for all of it's calls, make it a field of type B :

public class A {

    private B instance;

    //Constructors, setters and getters for b as necessary    

    public int methodThatNeedsABInstance() {
        int x = instance.get(); //Can call get method
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM