MainActivity is as follows:
[Java]
Package cn. testchangevar;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. app. Activity;
/**
* Demo description:
* Passing and changing values in Java
*
*/
Public class MainActivity extends Activity {
Private int id = 7;
Private Person mPerson;
Private Button mButton;
Private String mString;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MString = new String ("OOOOO ");
MPerson = new Person ("xx", 22 );
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View view ){
System. out. println ("111 id =" + id );
System. out. println ("111 mString =" + mString );
System. out. println ("111 mPerson. getAge () =" + mPerson. getAge ());
System. out. println ("======================= ");
Util. change (id );
Util. change (mString );
Util. change (mPerson );
System. out. println ("======================= ");
System. out. println ("222 id =" + id );
System. out. println ("222 mString =" + mString );
System. out. println ("222 mPerson. getAge () =" + mPerson. getAge ());
}
});
}
}
Package cn. testchangevar;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. app. Activity;
/**
* Demo description:
* Passing and changing values in Java
*
*/
Public class MainActivity extends Activity {
Private int id = 7;
Private Person mPerson;
Private Button mButton;
Private String mString;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MString = new String ("OOOOO ");
MPerson = new Person ("xx", 22 );
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View view ){
System. out. println ("111 id =" + id );
System. out. println ("111 mString =" + mString );
System. out. println ("111 mPerson. getAge () =" + mPerson. getAge ());
System. out. println ("======================= ");
Util. change (id );
Util. change (mString );
Util. change (mPerson );
System. out. println ("======================= ");
System. out. println ("222 id =" + id );
System. out. println ("222 mString =" + mString );
System. out. println ("222 mPerson. getAge () =" + mPerson. getAge ());
}
});
}
}
Util is as follows:
[Java]
Package cn. testchangevar;
Public class Util {
// It cannot be changed because int belongs to the basic type.
Public static void change (int I ){
I = 9527;
System. out. println ("has been changed I =" + I );
}
// It can be changed. The object is passed.
Public static void change (Person person ){
Person. setAge (88 );
System. out. println ("has been changed string =" + person );
}
// It cannot be changed. Although String is also an object, it is special
Public static void change (String string ){
String = "XXXXX ";
System. out. println ("has been changed string =" + string );
}
}
Package cn. testchangevar;
Public class Util {
// It cannot be changed because int belongs to the basic type.
Public static void change (int I ){
I = 9527;
System. out. println ("has been changed I =" + I );
}
// It can be changed. The object is passed.
Public static void change (Person person ){
Person. setAge (88 );
System. out. println ("has been changed string =" + person );
}
// It cannot be changed. Although String is also an object, it is special
Public static void change (String string ){
String = "XXXXX ";
System. out. println ("has been changed string =" + string );
}
}
Person:
[Java]
Package cn. testchangevar;
Public class Person {
Private String name;
Private int age;
Public Person (String name, int age ){
Super ();
This. name = name;
This. age = age;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getAge (){
Return age;
}
Public void setAge (int age ){
This. age = age;
}
@ Override
Public String toString (){
Return "Person [name =" + name + ", age =" + age + "]";
}
}
Package cn. testchangevar;
Public class Person {
Private String name;
Private int age;
Public Person (String name, int age ){
Super ();
This. name = name;
This. age = age;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getAge (){
Return age;
}
Public void setAge (int age ){
This. age = age;
}
@ Override
Public String toString (){
Return "Person [name =" + name + ", age =" + age + "]";
}
}
Main. xml is as follows:
[Html]
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>
<Button
Android: id = "@ + id/button"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"
Android: layout_centerInParent = "true"
/>
</RelativeLayout>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>
<Button
Android: id = "@ + id/button"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"
Android: layout_centerInParent = "true"
/>
</RelativeLayout>