Copy codeThe Code is 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); // The output here is 30! Normal Results
/*
* 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 (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;
}
If the following code is used: System. out. println ("After: percent =" + percent); // The output here is 10! Because static methods cannot achieve your desired results
This is because static methods cannot produce effects on objects. They are the same as static fields,It belongs to a class and does not belong to any object..
Copy codeThe Code is as follows :/**
* This program demonstrates parameter passing in Java.
* @ Version 1.00
* @ 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;
}