SoFunction
Updated on 2025-03-08

javz notes: the use of interesting static methods

Copy the codeThe code is as follows:

import .*;

public class welcome {

    public static void main(String[] args)
       {
          /*
           * Test 1: Methods can't modify numeric parameters
           */
          ("Testing tripleValue:");
          double percent = 10;
          ("Before: percent =" + percent);
          percent = tripleValue(percent);
("After: percent =" + percent);  //The output here is 30! Normal results

          /*
           * Test 2: Methods can change the state of object parameters
           */
          ("\nTesting tripleSalary:");
          Employee harry = new Employee("Harry", 50000);
          ("Before: salary =" + ());
          tripleSalary(harry);
          ("After: salary =" + ());

          /*
           * Test 3: Methods can't attach new objects to object parameters
           */
          ("\nTesting swap:");
          Employee a = new Employee("Alice", 70000);
          Employee b = new Employee("Bob", 60000);
          ("Before: a  =" + ());
          ("Before: b  =" + ());
          swap(a, b);
          ("After: a=" + ());
          ("After: b=" + ());
       }

       public static double tripleValue(double x) // doesn't work
       {
          return x = 3 * x;
          //("End of method: x=" + x);
       }

       public static void tripleSalary(Employee x) // works
       {
          (200);
          ("End of method: salary=" + ());
       }

       public static void swap(Employee x, Employee y)
       {
          Employee temp = x;
          x = y;
          y = temp;
          ("End of method: x=" + ());
          ("End of method: y=" + ());
       }
    }

    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: ("After: percent =" + percent);  //The output here is 10! Because the static method cannot achieve the effect you want

This is because static methods cannot have an effect on objects, just like static fields.It belongs to a class, not to any object

Copy the codeThe code is as follows:

/**
 * This program 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
       */
      ("Testing tripleValue:");
      double percent = 10;
      ("Before: percent=" + percent);
      tripleValue(percent);
      ("After: percent=" + percent);

      /*
       * Test 2: Methods can change the state of object parameters
       */
      ("\nTesting tripleSalary:");
      Employee harry = new Employee("Harry", 50000);
      ("Before: salary=" + ());
      tripleSalary(harry);
      ("After: salary=" + ());

      /*
       * Test 3: Methods can't attach new objects to object parameters
       */
      ("\nTesting swap:");
      Employee a = new Employee("Alice", 70000);
      Employee b = new Employee("Bob", 60000);
      ("Before: a=" + ());
      ("Before: b=" + ());
      swap(a, b);
      ("After: a=" + ());
      ("After: b=" + ());
   }

   public static void tripleValue(double x) // doesn't work
   {
      x = 3 * x;
      ("End of method: x=" + x);
   }

   public static void tripleSalary(Employee x) // works
   {
      (200);
      ("End of method: salary=" + ());
   }

   public static void swap(Employee x, Employee y)
   {
      Employee temp = x;
      x = y;
      y = temp;
      ("End of method: x=" + ());
      ("End of method: y=" + ());
   }
}

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