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.
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;
}
}