Considering another supposed class called Usandouserinfo.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
}
}
Ready, you used the get and set of your User_info class to set and take 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 reference stored by reference variable info
with their other classes
You can create a reference and by calling other classes you pass this reference to them, example:
Usandouserinfo.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);
}
}
Upshot:
Value assigned by Usandouserinfo: 44
Value assigned by Usandouserinfo2: 55
2) Make your User_info class a Singleton
Probably the most correct and bug-free, here you make use of the Singleton project standard, 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 need to basically define a private constructor in your User_info class and control its access. That is, only those who will call this builder is the class itself. So you guarantee that you will only create a single instance of it, which is when the reference variable is null, after that you return the already created variable instead of creating a new one.
To provide this reference created by your own class you must 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;
}
}
Usandouserinfo.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);
}
}
Upshot:
Value assigned by Usandouserinfo: 44
Value assigned by Usandouserinfo2: 55
3) Set your User_info class variables to be static
.
Define as static
It is easier however I believe that it is the worst of the options, because it hurts the principle of encapsulation. As I do not know for what you need, it does not cost to show the possibildiade:
User_info.java
public class User_Info {
public static int user_id;
public static String nickname;
}
Usandouserinfo.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;
}
}
Upshot:
Value assigned by Usandouserinfo: 44
Value assigned by Usandouserinfo2: 55
post the User_info.java code
– Math
don’t need the code...
– Lucas Bertollo
You definitely don’t need the memory address of
info
. I asked for your code so that it is possible to know more details and suddenly better direct the answer to your question.– Math
this there , I urge this at the beginning of the program but in the course of time I need to change these values in other classes , how will I access these data that are already in memory?
– Lucas Bertollo
@Lucasbertollo come on, if you can put example 1 at least of the other classes too, please ?
– user6026
@Harrypotter looks at the comments in my reply, he wants a shared reference of the same class in several other classes, or maybe define the attributes statically.
– Math
Thanks @Math, got it let’s see the unwind rs
– user6026
@Math THAT.. could you tell me more about this shared reference?
– Lucas Bertollo
@Math he doesn’t want Singleton?
– user6026
@Harrypotter also, are 3 options
– Math
@Math would like to know the 3 ahaha
– Lucas Bertollo