0

So I have a class with an int attribute which can't be static:

public class GetterId{

    int id = 42;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

And I'd like to have an access to this "id" attribute from another class, like:

public class MainActivity
{
   int id_bis;

   id_bis = GetterID.getId();
}

But it can't be this way because the method getId() and the attribute from the GetterId class are non static...

Is there any solutions to this problem?

Chris
  • 11
  • 1
  • 1
  • 5
  • The question doesn't make sense. The class doesn't have any non-static attributes. Only instances have those. You want the attribute, you need an instance. – user207421 Sep 15 '15 at 21:10

3 Answers3

1

Create a object of the GetterId class in the MainActivity class. With this object you can access variables and methods of the the GetterId class.

GetterId object = new GetterId();
object.getId();
object.setId(34);
int id = object.id;
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46
  • What if MainActivity and GetterId class are in different packages? – Onik Sep 15 '15 at 20:29
  • just import the package name of the class. see this thread http://stackoverflow.com/questions/3480389/java-how-to-use-classes-in-other-package – Deepak Goyal Sep 15 '15 at 20:33
  • The reference you've provided deal with public methods. For the reference: [Controlling Access to Members of a Class](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) and [Isn't “package private” member access synonymous with the default (no-modifier) access?](http://stackoverflow.com/questions/5416074/isnt-package-private-member-access-synonymous-with-the-default-no-modifier) – Onik Sep 15 '15 at 20:46
0

You have to create an object of type GetterId and then call getId from this object.

    GetterId obj = new GetterId();
    obj.getId();

This is because your id has to be associated with an object since each object can have a different id. I guess this is why you have a setter function for the id. So for your program to retrieve the correct id it has to have a reference to an object of type GetterId.

Amr
  • 792
  • 3
  • 15
  • But what if I want to get the value of the id and not the object itself? – Chris Sep 15 '15 at 20:29
  • 1
    To get the id you have to have an instance of `GetterId` to call `getId()` on, assuming your class is set up like it is in your question. – Evan LaHurd Sep 15 '15 at 20:31
  • @Chris Then you have to make it static. There is no other way to access a value without creating an object unless you make it static. Static means that all object from that class share the same value this is why you can access it via the class name. But if each object has its own value then you have to access it through the object itself otherwise it won't be able to give you the correct value. – Amr Sep 15 '15 at 20:31
0

you could create a singleton class and use getInstance to get an instance of that object

example:

public final class GetterId {
    private static GetterId instance;

    private GetterId(){}

    public static GetterId getInstance(){
        if (instance == null) {
            synchronized (GetterId.class) {
                if (instance == null) {
                    instance = new GetterId();
                }
            }
        }
        return instance;
    }

}

Then anytime you need to reference that object just call

GetterId getter = GetterId.getInstance()
id_bis = getter.getId();

that should give you the same reference everywhere and you can use the object as normal

tyczj
  • 71,600
  • 54
  • 194
  • 296