First on the code:
1 ImportOrg.apache.commons.lang.builder.ToStringBuilder;2 ImportOrg.apache.commons.lang.builder.ToStringStyle;3 ImportOrg.slf4j.Logger;4 Importorg.slf4j.LoggerFactory;5 6 /**7 * Reference Delivery demo8 * @authorLishaohua9 * time:2016-3-24 pm 5:10:49Ten */ One Public classDemo1 { A Private StaticLogger Logger = Loggerfactory.getlogger (Demo1.class); - - //Set Properties ... the Public voidsetstudentparams (Student stu) { -Stu.setname ("Zhang San"); -Stu.setstuno ("0001"); - } + - /** + * Test ... A * @paramargs at */ - Public Static voidMain (string[] args) { -Demo1 Demo1 =NewDemo1 (); -Student stu =NewStudent ("0000", "John Doe"); -Logger.info ("Pre-update------>" +stu.tostring ()); - Demo1.setstudentparams (stu); inLogger.info ("Post-update------>" +stu.tostring ()); - } to + } - the /** * * Auxiliary class: Student class $ * Panax Notoginseng * @authorLishaohua - * time:2016-3-24 pm 5:13:58 the */ + classStudent { A the PrivateString Stuno;//School Number + - PrivateString name;//name $ $ //Default Constructs - PublicStudent () {} - the //Construction Method - Student (String Stuno, string name) {Wuyi Super(); the This. Stuno =Stuno; - This. Name =name; Wu } - About PublicString toString () { $ returnTostringbuilder.reflectiontostring ( This, Tostringstyle.short_prefix_style); - } - - PublicString Getstuno () { A returnStuno; + } the - Public voidSetstuno (String stuno) { $ This. Stuno =Stuno; the } the the PublicString GetName () { the returnname; - } in the Public voidsetName (String name) { the This. Name =name; About } the the}
Perform
17:43:26.593 [main] INFO c.s.masopen.integration.demo.demo1-Pre-update------>student[stuno=0000,name= John Doe] 17:43:26.598 [main] INFO c.s.masopen.integration.demo.demo1-Updated------>student[stuno=0001,name= Zhang San]
Analytical:
New Student ("0000", "John Doe"); Logger.info ("updated------>" + stu.tostring ());d Emo1.setstudentparams (Stu); Logger.info ("post-update------>" + stu.tostring ());
The first line creates 1 objects stu: Student stu = new Student ("0000", "John Doe");
The second line prints the object information: pre-update------>student[stuno=0000,name= John Doe]
The third line re-sets the object properties:
Line four Print object information: after update------>student[stuno=0001,name= Zhang San]
To illustrate the problem, we now make changes to the code:
Change Place 1:
// set properties ... Public void setstudentparams (Student stu) { new Student (); Stu.setname ("Zhang San"); Stu.setstuno ("0001"); }
It's simple: in the method of setting the property, we re-new the object passed over.
Re-run:
1 17:45:08.330 [main] INFO c.s.masopen.integration.demo.demo1-Pre-update------>student[stuno=0000, Name= John Doe]2 17:45:08.334 [main] INFO c.s.masopen.integration.demo.demo1-post-update------> Student[stuno=0000,name= John Doe]
Obviously, this time after the object has been reset by the property, no reference delivery occurs!
Try one step at a glance to see the value of the object:
The first line creates 1 objects stu: Student stu = new Student ("0000", "John Doe");
Notice the id=20 of the Stu object in the upper right corner
Second line print: before update------>student[stuno=0000,name= John Doe]
The third row is re-assigned, debugging information such as
Note that this method, because of a new object,stu the object's id=32
The four-line printing, debugging information such as:
Notice The id=20 of the Stu object in the upper right corner
Printing information: after the update------>student[stuno=0000,name= John Doe]
Analysis Summary:
Reference passing, during the entire object pass (method invocation), the object must remain unchanged (cannot be re-new), otherwise the entire object's properties will not be modified.
Java Object passing small parsing