0

Lets say I have a class User:

public class User {
    String userID;
    String password;
    Integer connectID;
    String name;

    public User(String Name, String ID, String Pass, Integer connect) {
        userID = ID;
        password = Pass;
        connectID = connect;
        name = Name;
    }

    public String getUserID() {
        return userID;
    }

    public String getPassword() {
        return password;
    }

    public Integer getConnectID() {
        return connectID;
    }

    public String getName() {
        return name;
    }
}

And I have a section of my code which takes the connectID of a certain object and puts it into a varaible connectionID = (accounts.get(i)).getConnectID(); where accounts is an ArrayList holding all of the objects created. Would there be a way for me to use the connectionID variable to relate back to the object again in another method textWindow.append("localhost." + ... + getDateTime() + " > "); where the ... part is the part that I want to use the getConnectID() method on.

5
  • 2
    Instead of defining connectID as an Integer, why don't you wrap it in an object containing the reference to the User object? Commented Jul 29, 2016 at 23:14
  • Can you include the relevant code of your question? Commented Jul 29, 2016 at 23:14
  • @TimBiegeleisen what other relevant code is there to include? Commented Jul 29, 2016 at 23:16
  • Declare connectionID with the type of User, and only call getConnectID when you need it. Commented Jul 29, 2016 at 23:17
  • Technically the answer is "yes, but you generally shouldn't." If you need to access the same object's fields multiple times, store that object as a variable, instead of storing the variable and then using it to find the object later. There are some exceptions, such as when a library limits input to a specific field (maybe the ID of an object), yet you still want to be able to get that object using the id (maybe in the case of user input, like a click, keyboard, or touch event). But this is fairly rare Commented Jul 29, 2016 at 23:46

3 Answers 3

1

Don't store connectionID as a variable. It is already stored within the User object. Instead, store the User as a variable so it's contents can be accessed again later:

//Before the for loop, in a wider scope, declare the User:
User user;
//Then, in the for loop, initialize it:
user = accounts.get(i);
//As it was declared outside the for loop, it can be accessed later:
textWindow.append("localhost." + "User ID: " + user.getConnectionID() + " at " + getDateTime() + " > ");//or however you wish to format it
Sign up to request clarification or add additional context in comments.

Comments

0

One possible solution here is to change the type of connectionID from Integer to a class you create

class ConnectionID {

    private final Integer id;
    private final User user;

    ConnectionID(final Integer id,
                 final User user) {
        this.id = id;
        this.user = user;
    }

    public getUser() {
        return this.user;
    }

}

Now you can relate back to the user, given a connection id.

Comments

0

Assuming that connectID is unique to each User. You could loop through the accounts and compare the connectID.

public User getUser(int connectID){
    for (User user : accounts){
        if (user.getConnectID()==connectID){
            return user;
        }
    }
    return null;
}

Comments

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.