Access data from classes already instantiated

3

I need to access data contained in class variables already instantiated from other classes

User_Info info = new User_Info()

I think I would need the address of the memory that is the "info" right?

public class User_Info {
    private int user_id;
    private String nickname;
    private Image profile_picture;
    private String first_name;
    private String last_name;
    private String country;
    private String gender;
    private String state;
    private Date date_nasc;

    public int getUser_id() {
        return user_id;
    }
    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public Image getProfile_picture() {
        return profile_picture;
    }
    public void setProfile_picture(Image profile_picture) {
        this.profile_picture = profile_picture;
    }
    public String getFirst_name() {
        return first_name;
    }
    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }
    public String getLast_name() {
        return last_name;
    }
    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getGender() {
        return gender;
    }
    public void setGender(String gender) {
        this.gender = gender;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public Date getDate_nasc() {
        return date_nasc;
    }
    public void setDate_nasc(Date date_nasc) {
        this.date_nasc = date_nasc;
    }
}
    
asked by anonymous 09.07.2014 / 19:32

3 answers

4

Given another supposed class called UsingUserInfo.java, which is in the same package as your User_Info class, do

public class UsandoUserInfo {
    public static void main(String[] args) {
        User_Info info = new User_Info();
        info.setUser_id(44); //definiu o valor da variável user_id como sendo 44
        System.out.println(info.getUser_id()); //pegou o valor e imprimiu 44 no seu console
    }
}

So, you used the get and set of your User_Info class to set and get the value of user_id

However, if you want to change the same attribute of your User_Info class from another class you have some alternatives, let's see:

1) Share the reference stored by the reference variable info with its other classes

You can create a reference and when calling other classes you pass this reference to them, eg:

Using UserInfo.java

public class UsandoUserInfo {
    public static void main(String[] args) {
        User_Info info = new User_Info();
        info.setUser_id(44);
        System.out.println("Valor atribuido pelo UsandoUserInfo: " + info.getUser_id());

        UsandoUserInfo2 u2 = new UsandoUserInfo2(info);
        u2.atribuiUserId();
        System.out.println("Valor atribuido pelo UsandoUserInfo2: " + info.getUser_id());
    }
}

Using UserInfo2.java

public class UsandoUserInfo2 {
    private User_Info info;
    public UsandoUserInfo2(User_Info info) {
        this.info = info;
    }
    public void atribuiUserId(){
        info.setUser_id(55);
    }
}

Result:

  

Value assigned by UsingUserInfo: 44
  Value Assigned by UsingUserInfo2: 55

2) Make your User_Info class a singleton

Probably the most correct and bug-free, here you make use of the Singleton design pattern, which is a way to ensure that there will never be more than one instance of your class in the same JVM.

To apply this pattern you basically need to define a private constructor in your User_Info class and control its access. That is, only who will call this constructor is the class itself. So you guarantee that you will only create a single instance of it, which will be when the reference variable is null, after that you return the already created variable instead of creating a new one.

To make this reference available by its own class available, you should make a static method that makes it available.

Example:

public class User_Info {
    private int user_id;
    private String nickname;
    private static User_Info info;

    private User_Info() {
    }

    public static User_Info getInstance() {
        if(info == null) {
            info = new User_Info();         
        }
        return info;
    }

    public int getUser_id() {
        return user_id;
    }
    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
}

Using UserInfo.java

public class UsandoUserInfo {
    public static void main(String[] args) {
        User_Info info = User_Info.getInstance();
        info.setUser_id(44);
        System.out.println("Valor atribuido pelo UsandoUserInfo: " + info.getUser_id());

        UsandoUserInfo2 u2 = new UsandoUserInfo2();
        u2.atribuiUserId();
        System.out.println("Valor atribuido pelo UsandoUserInfo2: " + info.getUser_id());
    }
}

Using UserInfo2.java

public class UsandoUserInfo2 {
    private User_Info info;

    public UsandoUserInfo2() {
        info = User_Info.getInstance();
    }

    public void atribuiUserId(){
        info.setUser_id(55);
    }
}

Result:

  

Value assigned by UsingUserInfo: 44
  Value Assigned by UsingUserInfo2: 55

Define your User_Info class variables to be static .

Setting as static is easier, though I think it's the worst of the options because it hurts the encapsulation principle. As I do not know for what you need, it does not cost shows the possibildiade:

User_Info.java

public class User_Info {
    public static int user_id;
    public static String nickname;
}

Using UserInfo.java

public class UsandoUserInfo {
    public static void main(String[] args) {
        User_Info.user_id = 44;
        System.out.println("Valor atribuido pelo UsandoUserInfo: " + User_Info.user_id);

        UsandoUserInfo2 u2 = new UsandoUserInfo2();
        u2.atribuiUserId();
        System.out.println("Valor atribuido pelo UsandoUserInfo2: " + User_Info.user_id);
    }
}

Using UserInfo2.java

public class UsandoUserInfo2 {
    public void atribuiUserId(){
        User_Info.user_id = 55;
    }
}

Result:

  

Value assigned by UsingUserInfo: 44
  Value Assigned by UsingUserInfo2: 55

    
09.07.2014 / 20:14
2

Use getter / setters as in a dummy variable minhaString defined within your User_Info class:

public class User_Info {

   private minhaString;

   public String getMinhaString(){
     return minhaString;
   }

   public void setMinhaString(String str){
      minhaString = str;
   }
}

And then to access:

User_Info info = new User_Info()
info.setMinhaString("uma String");
String s = info.getMinhaString();
//s terá o valor de "uma String"

Or, set the variable to public :

User_Info info = new User_Info()
info.minhaString = "uma String";
    
09.07.2014 / 20:10
0

Your User_Info class certainly has attributes and these attributes are encapsulated in getters and setters , correct?

To access the attribute data, just call getters . For example:

User_Info info = new User_Info();
info.setCodigo(1); // atribuirá o número '1' ao atributo 'codigo', supondo que seja um atributo do tipo int/Integer
System.out.println(info.getCodigo()); // imprimirá no console o que estiver guardado no atributo 'codigo'
    
09.07.2014 / 19:40