Java Object Reference and assignment

Source: Internet
Author: User

Case:

Package com.util;

Import java.util.ArrayList;
Import java.util.List;

public class Copyobjectutil {
private static list<string> List;
private static User usertemp;
static{
List=new arraylist<string> ();
List.add ("AA");
List.add ("BB");

Usertemp=new User ();
Usertemp.setname ("CA");
Usertemp.setremark ("original value");
Usertemp.setage (20);
}

public static void Main (string[] args) {
List<user> userlist=new arraylist<user> ();
for (int i=0;i<list.size (); i++) {
User User =new User ();
User=usertemp;
User.setname (List.get (i));
User.setage (User.getage () +i);
Userlist.add (user);
}
int i=0;
for (User o:userlist) {
System.out.println ("= = =" +i+++ "=======");
System.out.println (O.getname ());
System.out.println (O.getremark ());
System.out.println (O.getage ());
}

}

}
Class user{
private String name;
Private String remark;
private int age;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
Public String Getremark () {
return remark;
}
public void Setremark (String remark) {
This.remark = remark;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}

}
Output Result:

====0=======
Bb
Original value
21st
====1=======
Bb
Original value
21st

Problem: The value of the previous object in list is overwritten by the latter object from the result table item.

User User =new User ();
User=usertemp;

Cause: The address that points to the same object,list<user> references the same object, which is usertemp=new user (), the static block, and the for loop modifies it, actually modifies the same object, The previous value of the solid will appear after the phenomenon of the value;

Solution:

Plan

public static void Main (string[] args) throws exception{
List<user> userlist=new arraylist<user> ();
for (int i=0;i<list.size (); i++) {
User User =new User ();
User= (User) Beanutils.clonebean (usertemp);
User.setname (List.get (i));
User.setage (User.getage () +i);
Userlist.add (user);
}
int i=0;
for (User o:userlist) {
System.out.println ("= = =" +i+++ "=======");
System.out.println (O.getname ());
System.out.println (O.getremark ());
System.out.println (O.getage ());
}

}

Output:

====0=======
Aa
Original value
20
====1=======
Bb
Original value
21st

Solution 2--

public static void Main (string[] args) throws exception{
List<user> userlist=new arraylist<user> ();
for (int i=0;i<list.size (); i++) {
User User =new User ();
Beanutils.copyproperties (usertemp, user);
User.setname (List.get (i));
User.setage (User.getage () +i);
Userlist.add (user);
}
int i=0;
for (User o:userlist) {
System.out.println ("= = =" +i+++ "=======");
System.out.println (O.getname ());
System.out.println (O.getremark ());
System.out.println (O.getage ());
}

}

Output:

Aa
Original value
20
====1=======
Bb
Original value
21st




Java Object Reference and assignment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.