Copy Code code as follows:
Import java.util.*;
public class Welcome {
public static void Main (string[] args)
{
/*
* Test 1:methods can ' t modify numeric parameters
*/
System.out.println ("Testing triplevalue:");
double percent = 10;
System.out.println ("before:percent =" + percent);
percent = Triplevalue (percent);
System.out.println ("after:percent =" + percent); / /Here the output is 30! The normal result
/*
* Test 2:methods can change the state of object parameters
*/
System.out.println ("\ntesting triplesalary:");
Employee Harry = new Employee ("Harry", 50000);
System.out.println ("before:salary =" + harry.getsalary ());
Triplesalary (Harry);
System.out.println ("after:salary =" + harry.getsalary ());
/*
* Test 3:methods can ' t attach new objects to object parameters
*/
System.out.println ("\ntesting swap:");
Employee A = new Employee ("Alice", 70000);
Employee B = New Employee ("Bob", 60000);
System.out.println ("before:a =" + A.getname ());
System.out.println ("before:b =" + B.getname ());
Swap (A, b);
System.out.println ("after:a=" + a.getname ());
System.out.println ("after:b=" + b.getname ());
}
public static double Triplevalue (double x)//doesn ' t work
{
return x = 3 * x;
System.out.println ("End of method:x=" + x);
}
public static void Triplesalary (Employee x)//Works
{
x.raisesalary;
System.out.println ("End of method:salary=" + x.getsalary ());
}
public static void swap (employee x, employee y)
{
Employee temp = x;
x = y;
y = temp;
System.out.println ("End of method:x=" + x.getname ());
System.out.println ("End of method:y=" + y.getname ());
}
}
Class employee//Simplified employee class
{
Public Employee (String N, double s)
{
name = N;
Salary = s;
}
Public String GetName ()
{
return name;
}
Public double getsalary ()
{
return salary;
}
public void Raisesalary (double bypercent)
{
Double raise = salary * BYPERCENT/100;
Salary + = raise;
}
private String name;
private double salary;
}
If it is the following code: SYSTEM.OUT.PRINTLN ("after:percent =" + percent); The output here is 10! Because the static method doesn't achieve the effect you want.
This is because a static method cannot produce an effect on an object, and, like a static field, it belongs to a class and does not belong to any object .
Copy Code code as follows:
/**
* This program is demonstrates parameter passing in Java.
* @version 1.00 2000-01-27
* @author Cay Horstmann
*/
public class Paramtest
{
public static void Main (string[] args)
{
/*
* Test 1:methods can ' t modify numeric parameters
*/
SYSTEM.OUT.PRINTLN ("Testing triplevalue:");
Double percent = 10;
System.out.println ("before:percent=" + percent);
Triplevalue (percent);
System.out.println ("after:percent=" + percent);
/*
* Test 2:methods can change the state of object parameters
*/
System.out.println ("\ntesting triplesalary:");
Employee Harry = new Employee ("Harry", 50000);
System.out.println ("before:salary=" + harry.getsalary ());
Triplesalary (Harry);
System.out.println ("after:salary=" + harry.getsalary ());
/*
* Test 3:methods can ' t attach new objects To object Parameters
*/
System.out.println ("\ntesting swap:");
Employee A = new Employee ("Alice", 70000);
Employee B = New Employee ("Bob", 60000);
System.out.println ("before:a=" + a.getname ());
System.out.println ("before:b=" + b.getname ());
Swap (A, b);
System.out.println ("after:a=" + a.getname ());
System.out.println ("after:b=" + b.getname ());
}
public static void Triplevalue (double x)//doesn ' t work
{
x = 3 * x;
System.out.println ("End of method:x=" + x);
}
public static void Triplesalary (Employee x)//Works
{
X.raisesalary (200);
System.out.println ("End of method:salary=" + x.getsalary ());
}
public static void Swap (employee x, employee y)
{
Employee temp = x;
x = y;
y = temp;
System.out.println ("End of method:x=" + x.getname ());
System.out.println ("End of method:y=" + y.getname ());
}
}
Class employee//Simplified employee class
{
Public Employee (String N, double s)
{
name = N;
Salary = s;
}
Public String GetName ()
{
return name;
}
Public double getsalary ()
{
return salary;
}
public void Raisesalary (double bypercent)
{
Double raise = salary * BYPERCENT/100;
Salary + = raise;
}
private String name;
private double salary;
}