1

I am attempting to create get/set class with Java. However, I am having trouble getting the data back from the class afterwards.

public class ARNStorage {
    String arnStorage;

    public String getArnStorage() {
        System.out.println("Got endpoint: " + this.arnStorage);
        return arnStorage;
    }
    public void setArnStorage(String arnStorage) {
        this.arnStorage = arnStorage;
        System.out.println("Saved endpoint: " + this.arnStorage);
    }
}

To store the String, I use the following (this works)

public void storeEndpointArn(String endpointArn) {
    ARNStorage endPoint = new ARNStorage();
    endPoint.setArnStorage(endpointArn);
    System.out.println("Storing endpoint: " + endpointArn);
}

However, to retrieve the String, I attempt retrieve it this way

public String retrieveEndpointArn() {
    String endPointArn = ARNStorage.getArnStorage();
    System.out.println("Retrieved endpoint: " + endPointArn);
    return endPointArn;
}

However, this returns a non-static method getArnStorage() which cannot be retrieved from a static context. My understanding a static context is that it cannot be called on something that doesn't exist.

Iorek
  • 571
  • 1
  • 13
  • 31
  • 1
    You need to create an instance of the `ARNStorage` class if you want to use its non-static methods. – D M Apr 24 '17 at 19:36
  • Why do I need to declare a new instance of ARNStorage to retrieve the value? The instance has already been created in the storeEndpointArn() – Iorek Apr 24 '17 at 19:39
  • You don't. But you do need to USE that old instance. – D M Apr 24 '17 at 19:41
  • The thing is, if you say `ARNStorage.getArnStorage()`, it doesn't know which instance of `ARNStorage` you mean. You could have a thousand of them for all it knows. – D M Apr 24 '17 at 19:48

3 Answers3

2

You create a ARNStorage local variable in the storing method and in the retrieval method, you don't use a ARNStorage instance but the class itself.
It makes no sense.
You should use an instance in both cases and the same.

To achieve it, the ARNStorage endPoint should be a instance field of the class and not a local variable if you want to reuse it from another method.

For example, you could have :

public class ClientClass{

    private ARNStorage endPoint;

    public void storeEndpointArn(String endpointArn) {    
        endPoint = new ARNStorage();
        endPoint.setArnStorage(endpointArn);
        System.out.println("Storing endpoint: " + endpointArn);
    }

    public String retrieveEndpointArn() {
        String endPointArn = endPoint.getArnStorage();
        System.out.println("Retrieved endpoint: " + endPointArn);
        return endPointArn;
    }
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

You need to keep hold and use the same reference to store and retrieve the string.

ARNStorage endPoint = new ARNStorage();//endPoint is the reference to the object
endPoint.setArnStorage(endpointArn);

String endPointArn = endPoint.getArnStorage();//use endPoint to retrieve data
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • 1
    This will compile, but this won't do anything useful - he needs the *old* instance of the class that he previously stored something in. – D M Apr 24 '17 at 19:40
  • OK, that looks better after the edit. – D M Apr 24 '17 at 19:45
1
  1. You don't need storeEndpointArn and retrieveEndpointArn methods as storeEndpointArn and getArnStorage do exactly what you are trying to achieve via storeEndpointArn and retrieveEndpointArn respectively. You can try calling those getters and setters directly on instance instead of creating an object inside storeEndpointArn and setting the value (which does not do what you want it to do anyway).

  2. As far as static and non-static methods are concerned, you can't call an instance method with a class name, only static methods can be called like this. Have a look at this article for static vs non-static methods.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102