Javz notes: Use of interesting static methods

Source: Internet
Author: User

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;
}

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.