Javz notes: The use of interesting static methods _java

Source: Internet
Author: User

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

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.