1

Unfortunately my Java knowledge is somewhat lacking so this is somewhat of a beginner's question but I can't seem to figure out how I can do this.

Essentially I have a method that creates and populates an ArrayList. The said populated list is something that I want to use in a different method and potentially another class. I think that it is a class level variable I want to save it as but I'm not sure how to do this. The code below will make it more clear.

public class JenAcc {

private final String endpointUrl = "http://a-url/";  

private String uri = "api/json";

public static void main(String[] args) {

    JenAcc obj = new JenAcc();
    obj.jsonRun();

}

public void jsonRun() {

    ArrayList<String> abbrevList = new ArrayList<>();
    ArrayList<String> urlList = new ArrayList<>();

    try {
        RestServiceClient client = RestServiceClient.getClient(endpointUrl);
        Response response = client
                .path(uri)
                .accept(MediaType.APPLICATION_JSON)
                .get();

        String s = (String) response.getEntity();

        JobList jjj = JSONResponseHandler.mapJSONToResponse(s, JobList.class);

        for (int i = 0; i < jjj.getJobs().size(); i++) {
            if (jjj.getJobs().get(i).getName().contains("services"){
                abbrevList.add(jjj.getJobs().get(i).getName().replace("services", ""));
                urlList.add(jjj.getJobs().get(i).getUrl());

            } else i++;
        }

        System.out.println(abbrevList);
        System.out.println(urlList);

    } catch (Exception e) {
        e.printStackTrace();
    }
    }
}

So essentially I want to have a way to make use of that list outside of that method, in another method or class. How would I go about doing that?

Thanks for your patience.

2 Answers 2

3

Declare the lists as member variables, & define accessor methods to reach out to them from outside the class:

public class JenAcc {
    private List<String> abbrevList = new ArrayList<>();
    private List<String> urlList = new ArrayList<>();

    ...

    public List<String> getAbbrevList() {
        return abbrevList;
    }

    public List<String> geturlList() {
        return urlList;
    }
}

& to access the lists from outside the class :

JenAcc jenacc = new JenAcc(); // initialization
List<String> urls = jenacc.geturlList(); // Use the accessor method defined above
// Same goes for the other list
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response. I just have one question in regards to this. How do I make it so that the list in jsonRun() will store the added content into the declared one. As it stands, the method isn't linked to them.
These are just accessors, they are not supposed to be linked to jsonRun() in any way.
I see. Do you know of a standard approach I should take to have the method retrieve this or should I be returning the list in some other way to access the strings I want to add to it
the jsonRun() method shall modify the content of the arraylists. You don't need to return anything. Using the accessor methods (as defined above), you can access the arraylists any time you want, from any JenAcc instance.
Apologies for the multiple questions, just trying to get my head around the whole concept. I understand the getter as an accessor but I'm still unsure how to get the arraylist populated in the first place.
2

If it should be a class-level member, then just declare it where you declare your other class-level members:

private final String endpointUrl = "http://a-url/";  
private String uri = "api/json";

ArrayList<String> abbrevList = new ArrayList<>();
ArrayList<String> urlList = new ArrayList<>();

(And of course remove the local declarations from the method.) Naturally, this will affect the scope of those values. Running the method more than once on any given instance of the object could have unintended side-effects, but that's really up to the logic of whatever you need to do to/with these values.

At that point any other code in the object will be able to access those values. If they need to be accessed by other objects as well, add a getter:

public ArrayList<String> getAbbrevList() {
    return this.abbrevList;
}

There are of course other approaches as well, it all really depends on how your object should behave rather than how your method should behave. For example, maybe it shouldn't be a class-level member but should instead be returned by the method? It's a question for the semantics of the system you're building. And names like "JenAcc" and "jsonRun" don't really convey much about those semantics.

1 Comment

Thanks for the response. You're right in what you're saying at the end. It may be that I should return the ArrayList instead but I need to do some research around that. Alas, I have much to learn still!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.