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