You want to call Math.sqrt(16) without creating a Math object. Static methods belong to the class itself, not to instances. They're perfect for utility functions that don't need object state.

Create utility methods

Define helper methods that don't need instance data.

example
UtilityMethods.java
Replay: real traced execution (multi-file project)
public class UtilityMethods {
    public static void main(String[] args) {
        System.out.println("=== Static Utility Methods ===\n");

        // No object needed - call on class!
        int squareInput = 5;
        int cubeInput = 3;
        int evenInput = 7;
        System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));
        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
        System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
        System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));

        System.out.println("\n=== String Utilities ===");
        System.out.println("StringUtils.reverse(\"hello\") = " +
            StringUtils.reverse("hello"));
        System.out.println("StringUtils.isPalindrome(\"radar\") = " +
            StringUtils.isPalindrome("radar"));
        System.out.println("StringUtils.isPalindrome(\"hello\") = " +
            StringUtils.isPalindrome("hello"));

        // Constants
        System.out.println("\n=== Static Constants ===");
        System.out.println("MathUtils.PI = " + MathUtils.PI);
        System.out.println("MathUtils.E = " + MathUtils.E);
        System.out.println("Circle area (r=" + squareInput + "): " + MathUtils.circleArea(squareInput));
    }
}

class MathUtils {
    // Static constants
    public static final double PI = 3.14159265359;
    public static final double E = 2.71828182846;

    // Private constructor - prevent instantiation
    private MathUtils() {}

    static int square(int n) {
        return n * n;
    }

    static int cube(int n) {
        return n * n * n;
    }

    static boolean isEven(int n) {
        return n % 2 == 0;
    }

    static double circleArea(double radius) {
        return PI * radius * radius;
    }
}

class StringUtils {
    private StringUtils() {}

    static String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    static boolean isPalindrome(String s) {
        String clean = s.toLowerCase();
        return clean.equals(reverse(clean));
    }
}
public class UtilityMethods {
    public static void main(String[] args) {
        System.out.println("=== Static Utility Methods ===\n");

        // No object needed - call on class!
        int squareInput = 8;
        int cubeInput = 3;
        int evenInput = 7;
        System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));
        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
        System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
        System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));

        System.out.println("\n=== String Utilities ===");
        System.out.println("StringUtils.reverse(\"hello\") = " +
            StringUtils.reverse("hello"));
        System.out.println("StringUtils.isPalindrome(\"radar\") = " +
            StringUtils.isPalindrome("radar"));
        System.out.println("StringUtils.isPalindrome(\"hello\") = " +
            StringUtils.isPalindrome("hello"));

        // Constants
        System.out.println("\n=== Static Constants ===");
        System.out.println("MathUtils.PI = " + MathUtils.PI);
        System.out.println("MathUtils.E = " + MathUtils.E);
        System.out.println("Circle area (r=" + squareInput + "): " + MathUtils.circleArea(squareInput));
    }
}

class MathUtils {
    // Static constants
    public static final double PI = 3.14159265359;
    public static final double E = 2.71828182846;

    // Private constructor - prevent instantiation
    private MathUtils() {}

    static int square(int n) {
        return n * n;
    }

    static int cube(int n) {
        return n * n * n;
    }

    static boolean isEven(int n) {
        return n % 2 == 0;
    }

    static double circleArea(double radius) {
        return PI * radius * radius;
    }
}

class StringUtils {
    private StringUtils() {}

    static String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    static boolean isPalindrome(String s) {
        String clean = s.toLowerCase();
        return clean.equals(reverse(clean));
    }
}
public class UtilityMethods {
    public static void main(String[] args) {
        System.out.println("=== Static Utility Methods ===\n");

        // No object needed - call on class!
        int squareInput = 12;
        int cubeInput = 3;
        int evenInput = 7;
        System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));
        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
        System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
        System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));

        System.out.println("\n=== String Utilities ===");
        System.out.println("StringUtils.reverse(\"hello\") = " +
            StringUtils.reverse("hello"));
        System.out.println("StringUtils.isPalindrome(\"radar\") = " +
            StringUtils.isPalindrome("radar"));
        System.out.println("StringUtils.isPalindrome(\"hello\") = " +
            StringUtils.isPalindrome("hello"));

        // Constants
        System.out.println("\n=== Static Constants ===");
        System.out.println("MathUtils.PI = " + MathUtils.PI);
        System.out.println("MathUtils.E = " + MathUtils.E);
        System.out.println("Circle area (r=" + squareInput + "): " + MathUtils.circleArea(squareInput));
    }
}

class MathUtils {
    // Static constants
    public static final double PI = 3.14159265359;
    public static final double E = 2.71828182846;

    // Private constructor - prevent instantiation
    private MathUtils() {}

    static int square(int n) {
        return n * n;
    }

    static int cube(int n) {
        return n * n * n;
    }

    static boolean isEven(int n) {
        return n % 2 == 0;
    }

    static double circleArea(double radius) {
        return PI * radius * radius;
    }
}

class StringUtils {
    private StringUtils() {}

    static String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    static boolean isPalindrome(String s) {
        String clean = s.toLowerCase();
        return clean.equals(reverse(clean));
    }
}
public class UtilityMethods {
    public static void main(String[] args) {
        System.out.println("=== Static Utility Methods ===\n");

        // No object needed - call on class!
        int squareInput = 5;
        int cubeInput = 4;
        int evenInput = 7;
        System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));
        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
        System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
        System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));

        System.out.println("\n=== String Utilities ===");
        System.out.println("StringUtils.reverse(\"hello\") = " +
            StringUtils.reverse("hello"));
        System.out.println("StringUtils.isPalindrome(\"radar\") = " +
            StringUtils.isPalindrome("radar"));
        System.out.println("StringUtils.isPalindrome(\"hello\") = " +
            StringUtils.isPalindrome("hello"));

        // Constants
        System.out.println("\n=== Static Constants ===");
        System.out.println("MathUtils.PI = " + MathUtils.PI);
        System.out.println("MathUtils.E = " + MathUtils.E);
        System.out.println("Circle area (r=" + squareInput + "): " + MathUtils.circleArea(squareInput));
    }
}

class MathUtils {
    // Static constants
    public static final double PI = 3.14159265359;
    public static final double E = 2.71828182846;

    // Private constructor - prevent instantiation
    private MathUtils() {}

    static int square(int n) {
        return n * n;
    }

    static int cube(int n) {
        return n * n * n;
    }

    static boolean isEven(int n) {
        return n % 2 == 0;
    }

    static double circleArea(double radius) {
        return PI * radius * radius;
    }
}

class StringUtils {
    private StringUtils() {}

    static String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    static boolean isPalindrome(String s) {
        String clean = s.toLowerCase();
        return clean.equals(reverse(clean));
    }
}
public class UtilityMethods {
    public static void main(String[] args) {
        System.out.println("=== Static Utility Methods ===\n");

        // No object needed - call on class!
        int squareInput = 5;
        int cubeInput = 3;
        int evenInput = 8;
        System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));
        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
        System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
        System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));

        System.out.println("\n=== String Utilities ===");
        System.out.println("StringUtils.reverse(\"hello\") = " +
            StringUtils.reverse("hello"));
        System.out.println("StringUtils.isPalindrome(\"radar\") = " +
            StringUtils.isPalindrome("radar"));
        System.out.println("StringUtils.isPalindrome(\"hello\") = " +
            StringUtils.isPalindrome("hello"));

        // Constants
        System.out.println("\n=== Static Constants ===");
        System.out.println("MathUtils.PI = " + MathUtils.PI);
        System.out.println("MathUtils.E = " + MathUtils.E);
        System.out.println("Circle area (r=" + squareInput + "): " + MathUtils.circleArea(squareInput));
    }
}

class MathUtils {
    // Static constants
    public static final double PI = 3.14159265359;
    public static final double E = 2.71828182846;

    // Private constructor - prevent instantiation
    private MathUtils() {}

    static int square(int n) {
        return n * n;
    }

    static int cube(int n) {
        return n * n * n;
    }

    static boolean isEven(int n) {
        return n % 2 == 0;
    }

    static double circleArea(double radius) {
        return PI * radius * radius;
    }
}

class StringUtils {
    private StringUtils() {}

    static String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    static boolean isPalindrome(String s) {
        String clean = s.toLowerCase();
        return clean.equals(reverse(clean));
    }
}
public class UtilityMethods {
    public static void main(String[] args) {
        System.out.println("=== Static Utility Methods ===\n");

        // No object needed - call on class!
        int squareInput = 5;
        int cubeInput = 3;
        int evenInput = 13;
        System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));
        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
        System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
        System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));

        System.out.println("\n=== String Utilities ===");
        System.out.println("StringUtils.reverse(\"hello\") = " +
            StringUtils.reverse("hello"));
        System.out.println("StringUtils.isPalindrome(\"radar\") = " +
            StringUtils.isPalindrome("radar"));
        System.out.println("StringUtils.isPalindrome(\"hello\") = " +
            StringUtils.isPalindrome("hello"));

        // Constants
        System.out.println("\n=== Static Constants ===");
        System.out.println("MathUtils.PI = " + MathUtils.PI);
        System.out.println("MathUtils.E = " + MathUtils.E);
        System.out.println("Circle area (r=" + squareInput + "): " + MathUtils.circleArea(squareInput));
    }
}

class MathUtils {
    // Static constants
    public static final double PI = 3.14159265359;
    public static final double E = 2.71828182846;

    // Private constructor - prevent instantiation
    private MathUtils() {}

    static int square(int n) {
        return n * n;
    }

    static int cube(int n) {
        return n * n * n;
    }

    static boolean isEven(int n) {
        return n % 2 == 0;
    }

    static double circleArea(double radius) {
        return PI * radius * radius;
    }
}

class StringUtils {
    private StringUtils() {}

    static String reverse(String s) {
        return new StringBuilder(s).reverse().toString();
    }

    static boolean isPalindrome(String s) {
        String clean = s.toLowerCase();
        return clean.equals(reverse(clean));
    }
}
  1. squareInput ← 5, cubeInput ← 3, evenInput ← 7

    2public class UtilityMethods {3    public static void main(String[] args) {4        System.out.println("=== Static Utility Methods ===\n");5        6        // No object needed - call on class!  //?call7        int squareInput→ 5 = 5;  //@squareInput=5, 8, 128        int cubeInput→ 3 = 3;  //@cubeInput=3, 49        int evenInput→ 7 = 7;  //@evenInput=7, 8, 1310        System.out.println("MathUtils.square(" + squareInput5 + ") = " + MathUtils.square(squareInput));11        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
    output=== Static Utility Methods ===
  2. static int square(int n)

    39static int square(int n5) {40    return n5 * n;41}
  3. System.out.println("MathUtils.square(" + squareInput + ") = " + MathUt…

    9int evenInput = 7;  //@evenInput=7, 8, 1310System.out.println("MathUtils.square(" + squareInput5 + ") = " + MathUtils.square(squareInput));11System.out.println("MathUtils.cube(" + cubeInput3 + ") = " + MathUtils.cube(cubeInput));12System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
    outputMathUtils.square(5) = 25
  4. static int cube(int n)

    43static int cube(int n3) {44    return n3 * n * n;45}
  5. System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.…

    10System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));11System.out.println("MathUtils.cube(" + cubeInput3 + ") = " + MathUtils.cube(cubeInput));12System.out.println("MathUtils.isEven(" + evenInput7 + ") = " + MathUtils.isEven(evenInput));13System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.cube(3) = 27
  6. static boolean isEven(int n)

    pass 1 of 2
    47static boolean isEven(int n7) {48    return n7 % 2 == 0;49}
  7. System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtil…

    11System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));12System.out.println("MathUtils.isEven(" + evenInput7 + ") = " + MathUtils.isEven(evenInput));13System.out.println("MathUtils.isEven(" + (evenInput7 + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.isEven(7) = false
  8. static boolean isEven(int n)

    pass 2 of 2
    47static boolean isEven(int n8) {48    return n8 % 2 == 0;49}
  9. System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + Ma…

    12System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));13System.out.println("MathUtils.isEven(" + (evenInput7 + 1) + ") = " + MathUtils.isEven(evenInput + 1));1415System.out.println("\n=== String Utilities ===");16System.out.println("StringUtils.reverse(\"hello\") = " + 17    StringUtils.reverse("hello"));18System.out.println("StringUtils.isPalindrome(\"radar\") = " + 
    outputMathUtils.isEven(8) = true
    
    === String Utilities ===
  10. static String reverse(String s)

    pass 1 of 3
    59static String reverse(String shello) {60    return new StringBuilder(s).reverse().toString();61}
    All 3 passes — pass 1 is the card above
    passs
    1hello
    2radar
    3hello
  11. System.out.println("StringUtils.reverse(\"hello\") = " +

    15System.out.println("\n=== String Utilities ===");16System.out.println("StringUtils.reverse(\"hello\") = " + 17    StringUtils.reverse("hello"));18System.out.println("StringUtils.isPalindrome(\"radar\") = " + 19    StringUtils.isPalindrome("radar"));20System.out.println("StringUtils.isPalindrome(\"hello\") = " + 
    outputStringUtils.reverse("hello") = olleh
  12. clean ← radar

    pass 1 of 2
    63static boolean isPalindrome(String sradar) {64    String clean→ radar = s.toLowerCase();65    return clean.equals(reverse(cleanradar));66}
  13. System.out.println("StringUtils.isPalindrome(\"radar\") = " +

    17    StringUtils.reverse("hello"));18System.out.println("StringUtils.isPalindrome(\"radar\") = " + 19    StringUtils.isPalindrome("radar"));20System.out.println("StringUtils.isPalindrome(\"hello\") = " + 21    StringUtils.isPalindrome("hello"));
    outputStringUtils.isPalindrome("radar") = true
  14. clean ← hello

    pass 2 of 2
    63static boolean isPalindrome(String shello) {64    String clean→ hello = s.toLowerCase();65    return clean.equals(reverse(cleanhello));66}
  15. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    19        StringUtils.isPalindrome("radar"));20    System.out.println("StringUtils.isPalindrome(\"hello\") = " + 21        StringUtils.isPalindrome("hello"));22    23    // Constants  //@var=_,!24    System.out.println("\n=== Static Constants ===");25    System.out.println("MathUtils.PI = " + MathUtils.PI);26    System.out.println("MathUtils.E = " + MathUtils.E);27    System.out.println("Circle area (r=" + squareInput5 + "): " + MathUtils.circleArea(squareInput));28}
    outputStringUtils.isPalindrome("hello") = false
    
    === Static Constants ===
    MathUtils.PI = 3.14159265359
    MathUtils.E = 2.71828182846
  16. static double circleArea(double radius)

    51static double circleArea(double radius5.0) {  //@var=_,!52    return PI * radius5.0 * radius;  //@var=_,!53}  //@var=_,!
  17. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    26    System.out.println("MathUtils.E = " + MathUtils.E);27    System.out.println("Circle area (r=" + squareInput5 + "): " + MathUtils.circleArea(squareInput));28}
    outputCircle area (r=5): 78.53981633974999
  1. squareInput ← 8, cubeInput ← 3, evenInput ← 7

    1public class UtilityMethods {2    public static void main(String[] args) {3        System.out.println("=== Static Utility Methods ===\n");4        5        // No object needed - call on class!6        int squareInput→ 8 = 8;7        int cubeInput→ 3 = 3;8        int evenInput→ 7 = 7;9        System.out.println("MathUtils.square(" + squareInput8 + ") = " + MathUtils.square(squareInput));10        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
    output=== Static Utility Methods ===
  2. static int square(int n)

    38static int square(int n8) {39    return n8 * n;40}
  3. System.out.println("MathUtils.square(" + squareInput + ") = " + MathUt…

    8int evenInput = 7;9System.out.println("MathUtils.square(" + squareInput8 + ") = " + MathUtils.square(squareInput));10System.out.println("MathUtils.cube(" + cubeInput3 + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
    outputMathUtils.square(8) = 64
  4. static int cube(int n)

    42static int cube(int n3) {43    return n3 * n * n;44}
  5. System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.…

    9System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));10System.out.println("MathUtils.cube(" + cubeInput3 + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput7 + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.cube(3) = 27
  6. static boolean isEven(int n)

    pass 1 of 2
    46static boolean isEven(int n7) {47    return n7 % 2 == 0;48}
  7. System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtil…

    10System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput7 + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput7 + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.isEven(7) = false
  8. static boolean isEven(int n)

    pass 2 of 2
    46static boolean isEven(int n8) {47    return n8 % 2 == 0;48}
  9. System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + Ma…

    11System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput7 + 1) + ") = " + MathUtils.isEven(evenInput + 1));1314System.out.println("\n=== String Utilities ===");15System.out.println("StringUtils.reverse(\"hello\") = " + 16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 
    outputMathUtils.isEven(8) = true
    
    === String Utilities ===
  10. static String reverse(String s)

    pass 1 of 3
    58static String reverse(String shello) {59    return new StringBuilder(s).reverse().toString();60}
    All 3 passes — pass 1 is the card above
    passs
    1hello
    2radar
    3hello
  11. System.out.println("StringUtils.reverse(\"hello\") = " +

    14System.out.println("\n=== String Utilities ===");15System.out.println("StringUtils.reverse(\"hello\") = " + 16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 18    StringUtils.isPalindrome("radar"));19System.out.println("StringUtils.isPalindrome(\"hello\") = " + 
    outputStringUtils.reverse("hello") = olleh
  12. clean ← radar

    pass 1 of 2
    62static boolean isPalindrome(String sradar) {63    String clean→ radar = s.toLowerCase();64    return clean.equals(reverse(cleanradar));65}
  13. System.out.println("StringUtils.isPalindrome(\"radar\") = " +

    16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 18    StringUtils.isPalindrome("radar"));19System.out.println("StringUtils.isPalindrome(\"hello\") = " + 20    StringUtils.isPalindrome("hello"));
    outputStringUtils.isPalindrome("radar") = true
  14. clean ← hello

    pass 2 of 2
    62static boolean isPalindrome(String shello) {63    String clean→ hello = s.toLowerCase();64    return clean.equals(reverse(cleanhello));65}
  15. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    18        StringUtils.isPalindrome("radar"));19    System.out.println("StringUtils.isPalindrome(\"hello\") = " + 20        StringUtils.isPalindrome("hello"));21    22    // Constants23    System.out.println("\n=== Static Constants ===");24    System.out.println("MathUtils.PI = " + MathUtils.PI);25    System.out.println("MathUtils.E = " + MathUtils.E);26    System.out.println("Circle area (r=" + squareInput8 + "): " + MathUtils.circleArea(squareInput));27}
    outputStringUtils.isPalindrome("hello") = false
    
    === Static Constants ===
    MathUtils.PI = 3.14159265359
    MathUtils.E = 2.71828182846
  16. static double circleArea(double radius)

    50static double circleArea(double radius8.0) {51    return PI * radius8.0 * radius;52}
  17. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    25    System.out.println("MathUtils.E = " + MathUtils.E);26    System.out.println("Circle area (r=" + squareInput8 + "): " + MathUtils.circleArea(squareInput));27}
    outputCircle area (r=8): 201.06192982976
  1. squareInput ← 12, cubeInput ← 3, evenInput ← 7

    1public class UtilityMethods {2    public static void main(String[] args) {3        System.out.println("=== Static Utility Methods ===\n");4        5        // No object needed - call on class!6        int squareInput→ 12 = 12;7        int cubeInput→ 3 = 3;8        int evenInput→ 7 = 7;9        System.out.println("MathUtils.square(" + squareInput12 + ") = " + MathUtils.square(squareInput));10        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
    output=== Static Utility Methods ===
  2. static int square(int n)

    38static int square(int n12) {39    return n12 * n;40}
  3. System.out.println("MathUtils.square(" + squareInput + ") = " + MathUt…

    8int evenInput = 7;9System.out.println("MathUtils.square(" + squareInput12 + ") = " + MathUtils.square(squareInput));10System.out.println("MathUtils.cube(" + cubeInput3 + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
    outputMathUtils.square(12) = 144
  4. static int cube(int n)

    42static int cube(int n3) {43    return n3 * n * n;44}
  5. System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.…

    9System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));10System.out.println("MathUtils.cube(" + cubeInput3 + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput7 + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.cube(3) = 27
  6. static boolean isEven(int n)

    pass 1 of 2
    46static boolean isEven(int n7) {47    return n7 % 2 == 0;48}
  7. System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtil…

    10System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput7 + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput7 + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.isEven(7) = false
  8. static boolean isEven(int n)

    pass 2 of 2
    46static boolean isEven(int n8) {47    return n8 % 2 == 0;48}
  9. System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + Ma…

    11System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput7 + 1) + ") = " + MathUtils.isEven(evenInput + 1));1314System.out.println("\n=== String Utilities ===");15System.out.println("StringUtils.reverse(\"hello\") = " + 16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 
    outputMathUtils.isEven(8) = true
    
    === String Utilities ===
  10. static String reverse(String s)

    pass 1 of 3
    58static String reverse(String shello) {59    return new StringBuilder(s).reverse().toString();60}
    All 3 passes — pass 1 is the card above
    passs
    1hello
    2radar
    3hello
  11. System.out.println("StringUtils.reverse(\"hello\") = " +

    14System.out.println("\n=== String Utilities ===");15System.out.println("StringUtils.reverse(\"hello\") = " + 16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 18    StringUtils.isPalindrome("radar"));19System.out.println("StringUtils.isPalindrome(\"hello\") = " + 
    outputStringUtils.reverse("hello") = olleh
  12. clean ← radar

    pass 1 of 2
    62static boolean isPalindrome(String sradar) {63    String clean→ radar = s.toLowerCase();64    return clean.equals(reverse(cleanradar));65}
  13. System.out.println("StringUtils.isPalindrome(\"radar\") = " +

    16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 18    StringUtils.isPalindrome("radar"));19System.out.println("StringUtils.isPalindrome(\"hello\") = " + 20    StringUtils.isPalindrome("hello"));
    outputStringUtils.isPalindrome("radar") = true
  14. clean ← hello

    pass 2 of 2
    62static boolean isPalindrome(String shello) {63    String clean→ hello = s.toLowerCase();64    return clean.equals(reverse(cleanhello));65}
  15. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    18        StringUtils.isPalindrome("radar"));19    System.out.println("StringUtils.isPalindrome(\"hello\") = " + 20        StringUtils.isPalindrome("hello"));21    22    // Constants23    System.out.println("\n=== Static Constants ===");24    System.out.println("MathUtils.PI = " + MathUtils.PI);25    System.out.println("MathUtils.E = " + MathUtils.E);26    System.out.println("Circle area (r=" + squareInput12 + "): " + MathUtils.circleArea(squareInput));27}
    outputStringUtils.isPalindrome("hello") = false
    
    === Static Constants ===
    MathUtils.PI = 3.14159265359
    MathUtils.E = 2.71828182846
  16. static double circleArea(double radius)

    50static double circleArea(double radius12.0) {51    return PI * radius12.0 * radius;52}
  17. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    25    System.out.println("MathUtils.E = " + MathUtils.E);26    System.out.println("Circle area (r=" + squareInput12 + "): " + MathUtils.circleArea(squareInput));27}
    outputCircle area (r=12): 452.38934211696005
  1. squareInput ← 5, cubeInput ← 4, evenInput ← 7

    1public class UtilityMethods {2    public static void main(String[] args) {3        System.out.println("=== Static Utility Methods ===\n");4        5        // No object needed - call on class!6        int squareInput→ 5 = 5;7        int cubeInput→ 4 = 4;8        int evenInput→ 7 = 7;9        System.out.println("MathUtils.square(" + squareInput5 + ") = " + MathUtils.square(squareInput));10        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
    output=== Static Utility Methods ===
  2. static int square(int n)

    38static int square(int n5) {39    return n5 * n;40}
  3. System.out.println("MathUtils.square(" + squareInput + ") = " + MathUt…

    8int evenInput = 7;9System.out.println("MathUtils.square(" + squareInput5 + ") = " + MathUtils.square(squareInput));10System.out.println("MathUtils.cube(" + cubeInput4 + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
    outputMathUtils.square(5) = 25
  4. static int cube(int n)

    42static int cube(int n4) {43    return n4 * n * n;44}
  5. System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.…

    9System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));10System.out.println("MathUtils.cube(" + cubeInput4 + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput7 + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.cube(4) = 64
  6. static boolean isEven(int n)

    pass 1 of 2
    46static boolean isEven(int n7) {47    return n7 % 2 == 0;48}
  7. System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtil…

    10System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput7 + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput7 + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.isEven(7) = false
  8. static boolean isEven(int n)

    pass 2 of 2
    46static boolean isEven(int n8) {47    return n8 % 2 == 0;48}
  9. System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + Ma…

    11System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput7 + 1) + ") = " + MathUtils.isEven(evenInput + 1));1314System.out.println("\n=== String Utilities ===");15System.out.println("StringUtils.reverse(\"hello\") = " + 16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 
    outputMathUtils.isEven(8) = true
    
    === String Utilities ===
  10. static String reverse(String s)

    pass 1 of 3
    58static String reverse(String shello) {59    return new StringBuilder(s).reverse().toString();60}
    All 3 passes — pass 1 is the card above
    passs
    1hello
    2radar
    3hello
  11. System.out.println("StringUtils.reverse(\"hello\") = " +

    14System.out.println("\n=== String Utilities ===");15System.out.println("StringUtils.reverse(\"hello\") = " + 16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 18    StringUtils.isPalindrome("radar"));19System.out.println("StringUtils.isPalindrome(\"hello\") = " + 
    outputStringUtils.reverse("hello") = olleh
  12. clean ← radar

    pass 1 of 2
    62static boolean isPalindrome(String sradar) {63    String clean→ radar = s.toLowerCase();64    return clean.equals(reverse(cleanradar));65}
  13. System.out.println("StringUtils.isPalindrome(\"radar\") = " +

    16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 18    StringUtils.isPalindrome("radar"));19System.out.println("StringUtils.isPalindrome(\"hello\") = " + 20    StringUtils.isPalindrome("hello"));
    outputStringUtils.isPalindrome("radar") = true
  14. clean ← hello

    pass 2 of 2
    62static boolean isPalindrome(String shello) {63    String clean→ hello = s.toLowerCase();64    return clean.equals(reverse(cleanhello));65}
  15. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    18        StringUtils.isPalindrome("radar"));19    System.out.println("StringUtils.isPalindrome(\"hello\") = " + 20        StringUtils.isPalindrome("hello"));21    22    // Constants23    System.out.println("\n=== Static Constants ===");24    System.out.println("MathUtils.PI = " + MathUtils.PI);25    System.out.println("MathUtils.E = " + MathUtils.E);26    System.out.println("Circle area (r=" + squareInput5 + "): " + MathUtils.circleArea(squareInput));27}
    outputStringUtils.isPalindrome("hello") = false
    
    === Static Constants ===
    MathUtils.PI = 3.14159265359
    MathUtils.E = 2.71828182846
  16. static double circleArea(double radius)

    50static double circleArea(double radius5.0) {51    return PI * radius5.0 * radius;52}
  17. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    25    System.out.println("MathUtils.E = " + MathUtils.E);26    System.out.println("Circle area (r=" + squareInput5 + "): " + MathUtils.circleArea(squareInput));27}
    outputCircle area (r=5): 78.53981633974999
  1. squareInput ← 5, cubeInput ← 3, evenInput ← 8

    1public class UtilityMethods {2    public static void main(String[] args) {3        System.out.println("=== Static Utility Methods ===\n");4        5        // No object needed - call on class!6        int squareInput→ 5 = 5;7        int cubeInput→ 3 = 3;8        int evenInput→ 8 = 8;9        System.out.println("MathUtils.square(" + squareInput5 + ") = " + MathUtils.square(squareInput));10        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
    output=== Static Utility Methods ===
  2. static int square(int n)

    38static int square(int n5) {39    return n5 * n;40}
  3. System.out.println("MathUtils.square(" + squareInput + ") = " + MathUt…

    8int evenInput = 8;9System.out.println("MathUtils.square(" + squareInput5 + ") = " + MathUtils.square(squareInput));10System.out.println("MathUtils.cube(" + cubeInput3 + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
    outputMathUtils.square(5) = 25
  4. static int cube(int n)

    42static int cube(int n3) {43    return n3 * n * n;44}
  5. System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.…

    9System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));10System.out.println("MathUtils.cube(" + cubeInput3 + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput8 + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.cube(3) = 27
  6. static boolean isEven(int n)

    pass 1 of 2
    46static boolean isEven(int n8) {47    return n8 % 2 == 0;48}
  7. System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtil…

    10System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput8 + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput8 + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.isEven(8) = true
  8. static boolean isEven(int n)

    pass 2 of 2
    46static boolean isEven(int n9) {47    return n9 % 2 == 0;48}
  9. System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + Ma…

    11System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput8 + 1) + ") = " + MathUtils.isEven(evenInput + 1));1314System.out.println("\n=== String Utilities ===");15System.out.println("StringUtils.reverse(\"hello\") = " + 16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 
    outputMathUtils.isEven(9) = false
    
    === String Utilities ===
  10. static String reverse(String s)

    pass 1 of 3
    58static String reverse(String shello) {59    return new StringBuilder(s).reverse().toString();60}
    All 3 passes — pass 1 is the card above
    passs
    1hello
    2radar
    3hello
  11. System.out.println("StringUtils.reverse(\"hello\") = " +

    14System.out.println("\n=== String Utilities ===");15System.out.println("StringUtils.reverse(\"hello\") = " + 16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 18    StringUtils.isPalindrome("radar"));19System.out.println("StringUtils.isPalindrome(\"hello\") = " + 
    outputStringUtils.reverse("hello") = olleh
  12. clean ← radar

    pass 1 of 2
    62static boolean isPalindrome(String sradar) {63    String clean→ radar = s.toLowerCase();64    return clean.equals(reverse(cleanradar));65}
  13. System.out.println("StringUtils.isPalindrome(\"radar\") = " +

    16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 18    StringUtils.isPalindrome("radar"));19System.out.println("StringUtils.isPalindrome(\"hello\") = " + 20    StringUtils.isPalindrome("hello"));
    outputStringUtils.isPalindrome("radar") = true
  14. clean ← hello

    pass 2 of 2
    62static boolean isPalindrome(String shello) {63    String clean→ hello = s.toLowerCase();64    return clean.equals(reverse(cleanhello));65}
  15. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    18        StringUtils.isPalindrome("radar"));19    System.out.println("StringUtils.isPalindrome(\"hello\") = " + 20        StringUtils.isPalindrome("hello"));21    22    // Constants23    System.out.println("\n=== Static Constants ===");24    System.out.println("MathUtils.PI = " + MathUtils.PI);25    System.out.println("MathUtils.E = " + MathUtils.E);26    System.out.println("Circle area (r=" + squareInput5 + "): " + MathUtils.circleArea(squareInput));27}
    outputStringUtils.isPalindrome("hello") = false
    
    === Static Constants ===
    MathUtils.PI = 3.14159265359
    MathUtils.E = 2.71828182846
  16. static double circleArea(double radius)

    50static double circleArea(double radius5.0) {51    return PI * radius5.0 * radius;52}
  17. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    25    System.out.println("MathUtils.E = " + MathUtils.E);26    System.out.println("Circle area (r=" + squareInput5 + "): " + MathUtils.circleArea(squareInput));27}
    outputCircle area (r=5): 78.53981633974999
  1. squareInput ← 5, cubeInput ← 3, evenInput ← 13

    1public class UtilityMethods {2    public static void main(String[] args) {3        System.out.println("=== Static Utility Methods ===\n");4        5        // No object needed - call on class!6        int squareInput→ 5 = 5;7        int cubeInput→ 3 = 3;8        int evenInput→ 13 = 13;9        System.out.println("MathUtils.square(" + squareInput5 + ") = " + MathUtils.square(squareInput));10        System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));
    output=== Static Utility Methods ===
  2. static int square(int n)

    38static int square(int n5) {39    return n5 * n;40}
  3. System.out.println("MathUtils.square(" + squareInput + ") = " + MathUt…

    8int evenInput = 13;9System.out.println("MathUtils.square(" + squareInput5 + ") = " + MathUtils.square(squareInput));10System.out.println("MathUtils.cube(" + cubeInput3 + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));
    outputMathUtils.square(5) = 25
  4. static int cube(int n)

    42static int cube(int n3) {43    return n3 * n * n;44}
  5. System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.…

    9System.out.println("MathUtils.square(" + squareInput + ") = " + MathUtils.square(squareInput));10System.out.println("MathUtils.cube(" + cubeInput3 + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput13 + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.cube(3) = 27
  6. static boolean isEven(int n)

    pass 1 of 2
    46static boolean isEven(int n13) {47    return n13 % 2 == 0;48}
  7. System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtil…

    10System.out.println("MathUtils.cube(" + cubeInput + ") = " + MathUtils.cube(cubeInput));11System.out.println("MathUtils.isEven(" + evenInput13 + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput13 + 1) + ") = " + MathUtils.isEven(evenInput + 1));
    outputMathUtils.isEven(13) = false
  8. static boolean isEven(int n)

    pass 2 of 2
    46static boolean isEven(int n14) {47    return n14 % 2 == 0;48}
  9. System.out.println("MathUtils.isEven(" + (evenInput + 1) + ") = " + Ma…

    11System.out.println("MathUtils.isEven(" + evenInput + ") = " + MathUtils.isEven(evenInput));12System.out.println("MathUtils.isEven(" + (evenInput13 + 1) + ") = " + MathUtils.isEven(evenInput + 1));1314System.out.println("\n=== String Utilities ===");15System.out.println("StringUtils.reverse(\"hello\") = " + 16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 
    outputMathUtils.isEven(14) = true
    
    === String Utilities ===
  10. static String reverse(String s)

    pass 1 of 3
    58static String reverse(String shello) {59    return new StringBuilder(s).reverse().toString();60}
    All 3 passes — pass 1 is the card above
    passs
    1hello
    2radar
    3hello
  11. System.out.println("StringUtils.reverse(\"hello\") = " +

    14System.out.println("\n=== String Utilities ===");15System.out.println("StringUtils.reverse(\"hello\") = " + 16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 18    StringUtils.isPalindrome("radar"));19System.out.println("StringUtils.isPalindrome(\"hello\") = " + 
    outputStringUtils.reverse("hello") = olleh
  12. clean ← radar

    pass 1 of 2
    62static boolean isPalindrome(String sradar) {63    String clean→ radar = s.toLowerCase();64    return clean.equals(reverse(cleanradar));65}
  13. System.out.println("StringUtils.isPalindrome(\"radar\") = " +

    16    StringUtils.reverse("hello"));17System.out.println("StringUtils.isPalindrome(\"radar\") = " + 18    StringUtils.isPalindrome("radar"));19System.out.println("StringUtils.isPalindrome(\"hello\") = " + 20    StringUtils.isPalindrome("hello"));
    outputStringUtils.isPalindrome("radar") = true
  14. clean ← hello

    pass 2 of 2
    62static boolean isPalindrome(String shello) {63    String clean→ hello = s.toLowerCase();64    return clean.equals(reverse(cleanhello));65}
  15. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    18        StringUtils.isPalindrome("radar"));19    System.out.println("StringUtils.isPalindrome(\"hello\") = " + 20        StringUtils.isPalindrome("hello"));21    22    // Constants23    System.out.println("\n=== Static Constants ===");24    System.out.println("MathUtils.PI = " + MathUtils.PI);25    System.out.println("MathUtils.E = " + MathUtils.E);26    System.out.println("Circle area (r=" + squareInput5 + "): " + MathUtils.circleArea(squareInput));27}
    outputStringUtils.isPalindrome("hello") = false
    
    === Static Constants ===
    MathUtils.PI = 3.14159265359
    MathUtils.E = 2.71828182846
  16. static double circleArea(double radius)

    50static double circleArea(double radius5.0) {51    return PI * radius5.0 * radius;52}
  17. System.out.println("Circle area (r=" + squareInput + "): " + MathUtils…

    25    System.out.println("MathUtils.E = " + MathUtils.E);26    System.out.println("Circle area (r=" + squareInput5 + "): " + MathUtils.circleArea(squareInput));27}
    outputCircle area (r=5): 78.53981633974999

static methods are called on the class: MathUtils.add(a, b).

static method Belongs to class, not instance. No `this` reference. Called via `ClassName.method()`.

Factory methods

Static methods that create and return objects.

FactoryPattern.java
Replay: real traced execution (multi-file project)
public class FactoryPattern {
    public static void main(String[] args) {
        System.out.println("=== Static Factory Methods ===\n");

        // Factory methods instead of constructors
        Point p1 = Point.fromCartesian(3, 4);
        Point p2 = Point.fromPolar(5, Math.PI / 4);  // 45 degrees
        Point p3 = Point.origin();

        System.out.println("Cartesian(3, 4): " + p1);
        System.out.println("Polar(5, 45°): " + p2);
        System.out.println("Origin: " + p3);

        System.out.println("\n=== User Factory ===");
        User guest = User.createGuest();
        User admin = User.createAdmin("alice");
        User member = User.create("bob", "member");

        System.out.println(guest);
        System.out.println(admin);
        System.out.println(member);
    }
}

class Point {
    final double x, y;

    // Private constructor - use factories
    private Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    // Factory: Cartesian coordinates
    static Point fromCartesian(double x, double y) {
        return new Point(x, y);
    }

    // Factory: Polar coordinates
    static Point fromPolar(double radius, double angleRadians) {
        double x = radius * Math.cos(angleRadians);
        double y = radius * Math.sin(angleRadians);
        return new Point(x, y);
    }

    // Factory: Cached common value
    private static final Point ORIGIN = new Point(0, 0);

    static Point origin() {
        return ORIGIN;  // Return same instance always
    }

    @Override
    public String toString() {
        return String.format("(%.2f, %.2f)", x, y);
    }
}

class User {
    final String name;
    final String role;

    private User(String name, String role) {
        this.name = name;
        this.role = role;
    }

    static User create(String name, String role) {
        return new User(name, role);
    }

    static User createGuest() {
        return new User("Guest", "guest");
    }

    static User createAdmin(String name) {
        return new User(name, "admin");
    }

    @Override
    public String toString() {
        return name + " (" + role + ")";
    }
}
  1. public static void main(String[] args)

    1public class FactoryPattern {2    public static void main(String[] args) {3        System.out.println("=== Static Factory Methods ===\n");4        5        // Factory methods instead of constructors  //?factory6        Point p1 = Point.fromCartesian(3, 4);7        Point p2 = Point.fromPolar(5, Math.PI / 4);  // 45 degrees
    output=== Static Factory Methods ===
  2. this.x ← 0.0, this.y ← 0.0

    pass 1 of 3
    28// Private constructor - use factories  //?private29private Point(double x0.0, double y0.0) {30    this.x→ 0.0 = x0.0;31    this.y→ 0.0 = y0.0;32}
    All 3 passes — pass 1 is the card above
    passradiusangleRadiansthis.xthis.yp1xyp2
    10.00.00.00.0
    25.00.78539816339744833.04.0(3.00, 4.00)3.53553390593273783.5355339059327373
    33.53553390593273783.53553390593273733.53553390593273783.5355339059327373(3.54, 3.54)
  3. static Point fromCartesian(double x, double y)

    34// Factory: Cartesian coordinates  //?cartesian35static Point fromCartesian(double x3.0, double y4.0) {36    return new Point(x, y);37}
  4. x ← 3.5355339059327378, y ← 3.5355339059327373

    39// Factory: Polar coordinates40static Point fromPolar(double radius5.0, double angleRadians0.7853981633974483) {41    double x→ 3.5355339059327378 = radius5.0 * Math.cos(angleRadians0.7853981633974483);42    double y→ 3.5355339059327373 = radius5.0 * Math.sin(angleRadians0.7853981633974483);43    return new Point(x, y);44}
  5. p3 ← (0.00, 0.00)

    7Point p2 = Point.fromPolar(5, Math.PI / 4);  // 45 degrees8Point p3→ (0.00, 0.00) = Point.origin();910System.out.println("Cartesian(3, 4): " + p1(3.00, 4.00));11System.out.println("Polar(5, 45°): " + p2(3.54, 3.54));12System.out.println("Origin: " + p3(0.00, 0.00));1314System.out.println("\n=== User Factory ===");  //?userfactory15User guest = User.createGuest();16User admin = User.createAdmin("alice");
    outputCartesian(3, 4): (3.00, 4.00)
    Polar(5, 45°): (3.54, 3.54)
    Origin: (0.00, 0.00)
    
    === User Factory ===
  6. this.name ← Guest, this.role ← guest, guest ← Guest (guest)

    pass 1 of 3
    14        System.out.println("\n=== User Factory ===");  //?userfactory15        User guest→ Guest (guest) = User.createGuest();16        User admin = User.createAdmin("alice");17        User member = User.create("bob", "member");18        19        System.out.println(guest);20        System.out.println(admin);21        System.out.println(member);22    }23}2425class Point {26    final double x, y;27    28    // Private constructor - use factories  //?private29    private Point(double x, double y) {30        this.x = x;31        this.y = y;32    }33    34    // Factory: Cartesian coordinates  //?cartesian35    static Point fromCartesian(double x, double y) {36        return new Point(x, y);37    }38    39    // Factory: Polar coordinates40    static Point fromPolar(double radius, double angleRadians) {41        double x = radius * Math.cos(angleRadians);42        double y = radius * Math.sin(angleRadians);43        return new Point(x, y);44    }45    46    // Factory: Cached common value  //?cached47    private static final Point ORIGIN = new Point(0, 0);48    49    static Point origin() {50        return ORIGIN;  // Return same instance always51    }52    53    @Override54    public String toString() {55        return String.format("(%.2f, %.2f)", x, y);56    }57}5859class User {60    final String name;61    final String role;62    63    private User(String nameGuest, String roleguest) {64        this.name→ Guest = nameGuest;65        this.role→ guest = roleguest;66    }
    All 3 passes — pass 1 is the card above
    passnamerolethis.namethis.roleguestadminmember
    1GuestguestGuestguestGuest (guest)
    2aliceadminaliceadminalice (admin)
    3bobmemberbobmemberGuest (guest)alice (admin)bob (member)
  7. static User createAdmin(String name)

    76static User createAdmin(String namealice) {77    return new User(name, "admin");78}
  8. static User create(String name, String role)

    68static User create(String namebob, String rolemember) {69    return new User(name, role);70}

Factory methods can have descriptive names and control object creation.

factory method Static method that creates objects: `User.fromEmail("x@y.com")`.

Static counter across instances

Track data shared by all instances of a class.

StaticCounter.java
Replay: real traced execution (multi-file project)
public class StaticCounter {
    public static void main(String[] args) {
        System.out.println("=== Static Variable: Shared Counter ===\n");

        System.out.println("Total robots created: " + Robot.getCount());

        // Create some robots
        Robot r1 = new Robot("Alpha");
        Robot r2 = new Robot("Beta");
        Robot r3 = new Robot("Gamma");

        System.out.println("\nTotal robots: " + Robot.getCount());

        System.out.println("\n=== Each Robot's ID ===");
        r1.introduce();
        r2.introduce();
        r3.introduce();

        System.out.println("\n=== Static vs Instance ===");
        System.out.println("Robot.count (static) = " + Robot.getCount());
        System.out.println("r1.name (instance) = \"" + r1.getName() + "\"");
        System.out.println("r2.name (instance) = \"" + r2.getName() + "\"");

        // Create more
        new Robot("Delta");
        new Robot("Epsilon");

        System.out.println("\nFinal count: " + Robot.getCount());
    }
}

class Robot {
    // Static variable - ONE copy shared by all instances
    private static int count = 0;

    // Instance variables - each Robot has its own
    private String name;
    private int id;

    Robot(String name) {
        this.name = name;
        count++;           // Increment shared counter
        this.id = count;   // Assign unique ID
        System.out.println("Created Robot #" + id + ": " + name);
    }

    void introduce() {
        System.out.println("I am " + name + ", Robot #" + id +
                          " of " + count + " total");
    }

    // Static method - accesses static variable
    static int getCount() {
        return count;
        // Cannot access 'name' or 'id' here!
    }

    String getName() {
        return name;
    }
}
  1. public static void main(String[] args)

    1public class StaticCounter {2    public static void main(String[] args) {3        System.out.println("=== Static Variable: Shared Counter ===\n");4        5        System.out.println("Total robots created: " + Robot.getCount());
    output=== Static Variable: Shared Counter ===
  2. static int getCount()

    pass 1 of 4
    52// Static method - accesses static variable  //?getter53static int getCount() {54    return count0;55    // Cannot access 'name' or 'id' here!
    All 4 passes — pass 1 is the card above
    passcount
    10
    23
    33
    45
  3. System.out.println("Total robots created: " + Robot.getCount());

    5System.out.println("Total robots created: " + Robot.getCount());67// Create some robots  //?create8Robot r1 = new Robot("Alpha");9Robot r2 = new Robot("Beta");
    outputTotal robots created: 0
  4. this.name ← Alpha, count ← 1, this.id ← 1, r1 ← ⟨Robot A⟩

    pass 1 of 5
    7        // Create some robots  //?create8        Robot r1→ ⟨Robot A⟩ = new Robot("Alpha");9        Robot r2 = new Robot("Beta");10        Robot r3 = new Robot("Gamma");11        12        System.out.println("\nTotal robots: " + Robot.getCount());  //?shared13        14        System.out.println("\n=== Each Robot's ID ===");15        r1.introduce();16        r2.introduce();17        r3.introduce();18        19        System.out.println("\n=== Static vs Instance ===");  //?compare20        System.out.println("Robot.count (static) = " + Robot.getCount());21        System.out.println("r1.name (instance) = \"" + r1.getName() + "\"");22        System.out.println("r2.name (instance) = \"" + r2.getName() + "\"");23        24        // Create more25        new Robot("Delta");26        new Robot("Epsilon");27        28        System.out.println("\nFinal count: " + Robot.getCount());29    }30}3132class Robot {33    // Static variable - ONE copy shared by all instances  //?static34    private static int count = 0;35    36    // Instance variables - each Robot has its own  //?instance37    private String name;38    private int id;39    40    Robot(String nameAlpha) {41        this.name→ Alpha = nameAlpha;42        count→ 1++;           // Increment shared counter43        this.id→ 1 = count1;   // Assign unique ID44        System.out.println("Created Robot #" + id1 + ": " + nameAlpha);45    }
    outputCreated Robot #1: Alpha
    All 5 passes — pass 1 is the card above
    passnameidthis.namecountthis.idr1r2r3
    1Alpha1Alpha0 11⟨Robot A⟩
    2Beta2Beta1 22⟨Robot B⟩
    3Gamma3Gamma2 33⟨Robot C⟩
    4Delta4Delta3 44
    5Epsilon5Epsilon4 55
  5. System.out.println(" Total robots: " + Robot.getCount()); //?shared

    12System.out.println("\nTotal robots: " + Robot.getCount());  //?shared1314System.out.println("\n=== Each Robot's ID ===");15r1.introduce();16r2.introduce();
    output
    Total robots: 3
    
    === Each Robot's ID ===
  6. void introduce()

    pass 1 of 3
    14        System.out.println("\n=== Each Robot's ID ===");15        r1.introduce();16        r2.introduce();17        r3.introduce();18        19        System.out.println("\n=== Static vs Instance ===");  //?compare20        System.out.println("Robot.count (static) = " + Robot.getCount());21        System.out.println("r1.name (instance) = \"" + r1.getName() + "\"");22        System.out.println("r2.name (instance) = \"" + r2.getName() + "\"");23        24        // Create more25        new Robot("Delta");26        new Robot("Epsilon");27        28        System.out.println("\nFinal count: " + Robot.getCount());29    }30}3132class Robot {33    // Static variable - ONE copy shared by all instances  //?static34    private static int count = 0;35    36    // Instance variables - each Robot has its own  //?instance37    private String name;38    private int id;39    40    Robot(String name) {41        this.name = name;42        count++;           // Increment shared counter43        this.id = count;   // Assign unique ID44        System.out.println("Created Robot #" + id + ": " + name);45    }46    47    void introduce() {  //?introduce48        System.out.println("I am " + nameAlpha + ", Robot #" + id1 + 49                          " of " + count3 + " total");50    }
    outputI am Alpha, Robot #1 of 3 total
    All 3 passes — pass 1 is the card above
    passnameid
    1Alpha1
    2Beta2
    3Gamma3
  7. System.out.println("Robot.count (static) = " + Robot.getCount());

    19System.out.println("\n=== Static vs Instance ===");  //?compare20System.out.println("Robot.count (static) = " + Robot.getCount());21System.out.println("r1.name (instance) = \"" + r1.getName() + "\"");22System.out.println("r2.name (instance) = \"" + r2.getName() + "\"");
    outputRobot.count (static) = 3
  8. String getName()

    pass 1 of 2
    58String getName() {59    return nameAlpha;60}
  9. System.out.println("r1.name (instance) = \"" + r1.getName() + "\"");

    20System.out.println("Robot.count (static) = " + Robot.getCount());21System.out.println("r1.name (instance) = \"" + r1.getName() + "\"");22System.out.println("r2.name (instance) = \"" + r2.getName() + "\"");
    outputr1.name (instance) = "Alpha"
  10. String getName()

    pass 2 of 2
    58String getName() {59    return nameBeta;60}
  11. System.out.println("r2.name (instance) = \"" + r2.getName() + "\"");

    21System.out.println("r1.name (instance) = \"" + r1.getName() + "\"");22System.out.println("r2.name (instance) = \"" + r2.getName() + "\"");2324// Create more25new Robot("Delta");26new Robot("Epsilon");
    outputr2.name (instance) = "Beta"
  12. System.out.println(" Final count: " + Robot.getCount());

    28    System.out.println("\nFinal count: " + Robot.getCount());29}
    output
    Final count: 5

Static variables are shared. Static methods can access static variables.

static variable Class-level variable shared by all instances. One copy for the whole class.

Static vs instance comparison

Understand when to use static vs instance methods.

StaticVsInstance.java
Replay: real traced execution (multi-file project)
public class StaticVsInstance {
    public static void main(String[] args) {
        System.out.println("=== Static vs Instance Methods ===\n");

        BankAccount acc1 = new BankAccount("Alice", 1000);
        BankAccount acc2 = new BankAccount("Bob", 500);

        // Instance methods - need object
        System.out.println("Instance method calls:");
        acc1.deposit(200);
        acc2.deposit(100);
        acc1.showBalance();
        acc2.showBalance();

        // Static methods - no object needed
        System.out.println("\nStatic method calls:");
        System.out.println("Total accounts: " + BankAccount.getTotalAccounts());
        System.out.println("Interest rate: " + BankAccount.getInterestRate() + "%");

        BankAccount.setInterestRate(3.5);
        System.out.println("New interest rate: " + BankAccount.getInterestRate() + "%");

        // Apply interest (uses both static and instance)
        System.out.println("\nApplying interest to all accounts:");
        acc1.applyInterest();
        acc2.applyInterest();
        acc1.showBalance();
        acc2.showBalance();
    }
}

class BankAccount {
    // Static members - shared by all accounts
    private static int totalAccounts = 0;
    private static double interestRate = 2.5;

    // Instance members - unique to each account
    private String owner;
    private double balance;

    BankAccount(String owner, double initialBalance) {
        this.owner = owner;
        this.balance = initialBalance;
        totalAccounts++;  // Affects all accounts
    }

    // Instance methods - work with this object's data
    void deposit(double amount) {
        this.balance += amount;
        System.out.println(owner + " deposited $" + amount);
    }

    void showBalance() {
        System.out.println(owner + "'s balance: $" + balance);
    }

    void applyInterest() {
        double interest = balance * (interestRate / 100);
        balance += interest;
        System.out.println(owner + " earned $" + String.format("%.2f", interest) + " interest");
    }

    // Static methods - work with class-level data only
    static int getTotalAccounts() {
        return totalAccounts;
        // Cannot access owner or balance here!
    }

    static double getInterestRate() {
        return interestRate;
    }

    static void setInterestRate(double rate) {
        interestRate = rate;
    }
}
  1. public static void main(String[] args)

    1public class StaticVsInstance {2    public static void main(String[] args) {3        System.out.println("=== Static vs Instance Methods ===\n");4        5        BankAccount acc1 = new BankAccount("Alice", 1000);6        BankAccount acc2 = new BankAccount("Bob", 500);
    output=== Static vs Instance Methods ===
  2. this.owner ← Alice, this.balance ← 1000.0, totalAccounts ← 1, acc1 ← ⟨BankAccount A⟩

    pass 1 of 2
    5        BankAccount acc1→ ⟨BankAccount A⟩ = new BankAccount("Alice", 1000);6        BankAccount acc2 = new BankAccount("Bob", 500);7        8        // Instance methods - need object  //?instance9        System.out.println("Instance method calls:");10        acc1.deposit(200);11        acc2.deposit(100);12        acc1.showBalance();13        acc2.showBalance();14        15        // Static methods - no object needed  //?static16        System.out.println("\nStatic method calls:");17        System.out.println("Total accounts: " + BankAccount.getTotalAccounts());18        System.out.println("Interest rate: " + BankAccount.getInterestRate() + "%");19        20        BankAccount.setInterestRate(3.5);  //?change21        System.out.println("New interest rate: " + BankAccount.getInterestRate() + "%");22        23        // Apply interest (uses both static and instance)  //?both24        System.out.println("\nApplying interest to all accounts:");25        acc1.applyInterest();26        acc2.applyInterest();27        acc1.showBalance();28        acc2.showBalance();29    }30}3132class BankAccount {33    // Static members - shared by all accounts34    private static int totalAccounts = 0;35    private static double interestRate = 2.5;  //?staticvar36    37    // Instance members - unique to each account38    private String owner;  //?instancevar39    private double balance;40    41    BankAccount(String ownerAlice, double initialBalance1000.0) {42        this.owner→ Alice = ownerAlice;43        this.balance→ 1000.0 = initialBalance1000.0;44        totalAccounts→ 1++;  // Affects all accounts45    }
  3. this.owner ← Bob, this.balance ← 500.0, totalAccounts ← 2, acc2 ← ⟨BankAccount B⟩

    pass 2 of 2
    5        BankAccount acc1 = new BankAccount("Alice", 1000);6        BankAccount acc2→ ⟨BankAccount B⟩ = new BankAccount("Bob", 500);7        8        // Instance methods - need object  //?instance9        System.out.println("Instance method calls:");10        acc1.deposit(200);11        acc2.deposit(100);12        acc1.showBalance();13        acc2.showBalance();14        15        // Static methods - no object needed  //?static16        System.out.println("\nStatic method calls:");17        System.out.println("Total accounts: " + BankAccount.getTotalAccounts());18        System.out.println("Interest rate: " + BankAccount.getInterestRate() + "%");19        20        BankAccount.setInterestRate(3.5);  //?change21        System.out.println("New interest rate: " + BankAccount.getInterestRate() + "%");22        23        // Apply interest (uses both static and instance)  //?both24        System.out.println("\nApplying interest to all accounts:");25        acc1.applyInterest();26        acc2.applyInterest();27        acc1.showBalance();28        acc2.showBalance();29    }30}3132class BankAccount {33    // Static members - shared by all accounts34    private static int totalAccounts = 0;35    private static double interestRate = 2.5;  //?staticvar36    37    // Instance members - unique to each account38    private String owner;  //?instancevar39    private double balance;40    41    BankAccount(String ownerBob, double initialBalance500.0) {42        this.owner→ Bob = ownerBob;43        this.balance→ 500.0 = initialBalance500.0;44        totalAccounts→ 2++;  // Affects all accounts45    }
    outputInstance method calls:
  4. this.balance ← 1200.0

    pass 1 of 2
    9        System.out.println("Instance method calls:");10        acc1.deposit(200);11        acc2.deposit(100);12        acc1.showBalance();13        acc2.showBalance();14        15        // Static methods - no object needed  //?static16        System.out.println("\nStatic method calls:");17        System.out.println("Total accounts: " + BankAccount.getTotalAccounts());18        System.out.println("Interest rate: " + BankAccount.getInterestRate() + "%");19        20        BankAccount.setInterestRate(3.5);  //?change21        System.out.println("New interest rate: " + BankAccount.getInterestRate() + "%");22        23        // Apply interest (uses both static and instance)  //?both24        System.out.println("\nApplying interest to all accounts:");25        acc1.applyInterest();26        acc2.applyInterest();27        acc1.showBalance();28        acc2.showBalance();29    }30}3132class BankAccount {33    // Static members - shared by all accounts34    private static int totalAccounts = 0;35    private static double interestRate = 2.5;  //?staticvar36    37    // Instance members - unique to each account38    private String owner;  //?instancevar39    private double balance;40    41    BankAccount(String owner, double initialBalance) {42        this.owner = owner;43        this.balance = initialBalance;44        totalAccounts++;  // Affects all accounts45    }46    47    // Instance methods - work with this object's data48    void deposit(double amount200.0) {  //?depositmethod49        this.balance→ 1200.0 += amount200.0;50        System.out.println(ownerAlice + " deposited $" + amount200.0);51    }
    outputAlice deposited $200.0
  5. this.balance ← 600.0

    pass 2 of 2
    10        acc1.deposit(200);11        acc2.deposit(100);12        acc1.showBalance();13        acc2.showBalance();14        15        // Static methods - no object needed  //?static16        System.out.println("\nStatic method calls:");17        System.out.println("Total accounts: " + BankAccount.getTotalAccounts());18        System.out.println("Interest rate: " + BankAccount.getInterestRate() + "%");19        20        BankAccount.setInterestRate(3.5);  //?change21        System.out.println("New interest rate: " + BankAccount.getInterestRate() + "%");22        23        // Apply interest (uses both static and instance)  //?both24        System.out.println("\nApplying interest to all accounts:");25        acc1.applyInterest();26        acc2.applyInterest();27        acc1.showBalance();28        acc2.showBalance();29    }30}3132class BankAccount {33    // Static members - shared by all accounts34    private static int totalAccounts = 0;35    private static double interestRate = 2.5;  //?staticvar36    37    // Instance members - unique to each account38    private String owner;  //?instancevar39    private double balance;40    41    BankAccount(String owner, double initialBalance) {42        this.owner = owner;43        this.balance = initialBalance;44        totalAccounts++;  // Affects all accounts45    }46    47    // Instance methods - work with this object's data48    void deposit(double amount100.0) {  //?depositmethod49        this.balance→ 600.0 += amount100.0;50        System.out.println(ownerBob + " deposited $" + amount100.0);51    }
    outputBob deposited $100.0
  6. void showBalance()

    pass 1 of 4
    11        acc2.deposit(100);12        acc1.showBalance();13        acc2.showBalance();14        15        // Static methods - no object needed  //?static16        System.out.println("\nStatic method calls:");17        System.out.println("Total accounts: " + BankAccount.getTotalAccounts());18        System.out.println("Interest rate: " + BankAccount.getInterestRate() + "%");19        20        BankAccount.setInterestRate(3.5);  //?change21        System.out.println("New interest rate: " + BankAccount.getInterestRate() + "%");22        23        // Apply interest (uses both static and instance)  //?both24        System.out.println("\nApplying interest to all accounts:");25        acc1.applyInterest();26        acc2.applyInterest();27        acc1.showBalance();28        acc2.showBalance();29    }30}3132class BankAccount {33    // Static members - shared by all accounts34    private static int totalAccounts = 0;35    private static double interestRate = 2.5;  //?staticvar36    37    // Instance members - unique to each account38    private String owner;  //?instancevar39    private double balance;40    41    BankAccount(String owner, double initialBalance) {42        this.owner = owner;43        this.balance = initialBalance;44        totalAccounts++;  // Affects all accounts45    }46    47    // Instance methods - work with this object's data48    void deposit(double amount) {  //?depositmethod49        this.balance += amount;50        System.out.println(owner + " deposited $" + amount);51    }52    53    void showBalance() {54        System.out.println(ownerAlice + "'s balance: $" + balance1200.0);55    }
    outputAlice's balance: $1200.0
    All 4 passes — pass 1 is the card above
    passownerbalancetotalAccounts
    1Alice1200.0
    2Bob600.02
    3Alice1242.0
    4Bob621.0
  7. static int getTotalAccounts()

    63// Static methods - work with class-level data only64static int getTotalAccounts() {  //?staticmethod65    return totalAccounts2;66    // Cannot access owner or balance here!
  8. System.out.println("Total accounts: " + BankAccount.getTotalAccounts()…

    16System.out.println("\nStatic method calls:");17System.out.println("Total accounts: " + BankAccount.getTotalAccounts());18System.out.println("Interest rate: " + BankAccount.getInterestRate() + "%");
    outputTotal accounts: 2
  9. static double getInterestRate()

    pass 1 of 2
    69static double getInterestRate() {70    return interestRate2.5;71}
  10. System.out.println("Interest rate: " + BankAccount.getInterestRate() +…

    17System.out.println("Total accounts: " + BankAccount.getTotalAccounts());18System.out.println("Interest rate: " + BankAccount.getInterestRate() + "%");1920BankAccount.setInterestRate(3.5);  //?change21System.out.println("New interest rate: " + BankAccount.getInterestRate() + "%");
    outputInterest rate: 2.5%
  11. interestRate ← 3.5

    20        BankAccount.setInterestRate(3.5);  //?change21        System.out.println("New interest rate: " + BankAccount.getInterestRate() + "%");22        23        // Apply interest (uses both static and instance)  //?both24        System.out.println("\nApplying interest to all accounts:");25        acc1.applyInterest();26        acc2.applyInterest();27        acc1.showBalance();28        acc2.showBalance();29    }30}3132class BankAccount {33    // Static members - shared by all accounts34    private static int totalAccounts = 0;35    private static double interestRate = 2.5;  //?staticvar36    37    // Instance members - unique to each account38    private String owner;  //?instancevar39    private double balance;40    41    BankAccount(String owner, double initialBalance) {42        this.owner = owner;43        this.balance = initialBalance;44        totalAccounts++;  // Affects all accounts45    }46    47    // Instance methods - work with this object's data48    void deposit(double amount) {  //?depositmethod49        this.balance += amount;50        System.out.println(owner + " deposited $" + amount);51    }52    53    void showBalance() {54        System.out.println(owner + "'s balance: $" + balance);55    }56    57    void applyInterest() {  //?hybrid58        double interest = balance * (interestRate / 100);59        balance += interest;60        System.out.println(owner + " earned $" + String.format("%.2f", interest) + " interest");61    }62    63    // Static methods - work with class-level data only64    static int getTotalAccounts() {  //?staticmethod65        return totalAccounts;66        // Cannot access owner or balance here!67    }68    69    static double getInterestRate() {70        return interestRate;71    }72    73    static void setInterestRate(double rate3.5) {74        interestRate→ 3.5 = rate3.5;75    }
  12. static double getInterestRate()

    pass 2 of 2
    69static double getInterestRate() {70    return interestRate3.5;71}
  13. System.out.println("New interest rate: " + BankAccount.getInterestRate…

    20BankAccount.setInterestRate(3.5);  //?change21System.out.println("New interest rate: " + BankAccount.getInterestRate() + "%");2223// Apply interest (uses both static and instance)  //?both24System.out.println("\nApplying interest to all accounts:");25acc1.applyInterest();26acc2.applyInterest();
    outputNew interest rate: 3.5%
    
    Applying interest to all accounts:
  14. interest ← 42.00000000000001, balance ← 1242.0

    pass 1 of 2
    24        System.out.println("\nApplying interest to all accounts:");25        acc1.applyInterest();26        acc2.applyInterest();27        acc1.showBalance();28        acc2.showBalance();29    }30}3132class BankAccount {33    // Static members - shared by all accounts34    private static int totalAccounts = 0;35    private static double interestRate = 2.5;  //?staticvar36    37    // Instance members - unique to each account38    private String owner;  //?instancevar39    private double balance;40    41    BankAccount(String owner, double initialBalance) {42        this.owner = owner;43        this.balance = initialBalance;44        totalAccounts++;  // Affects all accounts45    }46    47    // Instance methods - work with this object's data48    void deposit(double amount) {  //?depositmethod49        this.balance += amount;50        System.out.println(owner + " deposited $" + amount);51    }52    53    void showBalance() {54        System.out.println(owner + "'s balance: $" + balance);55    }56    57    void applyInterest() {  //?hybrid58        double interest→ 42.00000000000001 = balance1200.0 * (interestRate3.5 / 100);59        balance→ 1242.0 += interest42.00000000000001;60        System.out.println(ownerAlice + " earned $" + String.format("%.2f", interest42.00000000000001) + " interest");61    }
    outputAlice earned $42.00 interest
  15. interest ← 21.000000000000004, balance ← 621.0

    pass 2 of 2
    25        acc1.applyInterest();26        acc2.applyInterest();27        acc1.showBalance();28        acc2.showBalance();29    }30}3132class BankAccount {33    // Static members - shared by all accounts34    private static int totalAccounts = 0;35    private static double interestRate = 2.5;  //?staticvar36    37    // Instance members - unique to each account38    private String owner;  //?instancevar39    private double balance;40    41    BankAccount(String owner, double initialBalance) {42        this.owner = owner;43        this.balance = initialBalance;44        totalAccounts++;  // Affects all accounts45    }46    47    // Instance methods - work with this object's data48    void deposit(double amount) {  //?depositmethod49        this.balance += amount;50        System.out.println(owner + " deposited $" + amount);51    }52    53    void showBalance() {54        System.out.println(owner + "'s balance: $" + balance);55    }56    57    void applyInterest() {  //?hybrid58        double interest→ 21.000000000000004 = balance600.0 * (interestRate3.5 / 100);59        balance→ 621.0 += interest21.000000000000004;60        System.out.println(ownerBob + " earned $" + String.format("%.2f", interest21.000000000000004) + " interest");61    }
    outputBob earned $21.00 interest

Instance methods have this. Static methods don't - they can't access instance data.

Static import for cleaner code

Import static members directly into your namespace.

StaticImport.java
Replay: real traced execution (multi-file project)
// Static imports for cleaner code
import static java.lang.Math.PI;
import static java.lang.Math.sqrt;
import static java.lang.Math.pow;
import static java.lang.Math.sin;
import static java.lang.Math.cos;

public class StaticImport {
    public static void main(String[] args) {
        System.out.println("=== Static Import ===\n");

        // Without static import
        System.out.println("Without static import:");
        double area1 = java.lang.Math.PI * java.lang.Math.pow(5, 2);
        System.out.println("Circle area: " + area1);

        // With static import - cleaner!
        System.out.println("\nWith static import:");
        double area2 = PI * pow(5, 2);
        System.out.println("Circle area: " + area2);

        System.out.println("\n=== Geometry Calculations ===");

        double radius = 10;
        System.out.println("Radius: " + radius);
        System.out.println("Circumference: " + (2 * PI * radius));
        System.out.println("Area: " + (PI * pow(radius, 2)));

        // Pythagorean theorem
        double a = 3, b = 4;
        double c = sqrt(pow(a, 2) + pow(b, 2));
        System.out.println("\nPythagorean: sqrt(3² + 4²) = " + c);

        // Trigonometry
        double angle = PI / 4;  // 45 degrees
        System.out.println("\nAngle: 45° (π/4 radians)");
        System.out.println("sin(45°) = " + sin(angle));
        System.out.println("cos(45°) = " + cos(angle));
    }
}
  1. area1 ← 78.53981633974483, area2 ← 78.53981633974483, radius ← 10.0

    8public class StaticImport {9    public static void main(String[] args) {10        System.out.println("=== Static Import ===\n");11        12        // Without static import  //?without13        System.out.println("Without static import:");14        double area1→ 78.53981633974483 = java.lang.Math.PI * java.lang.Math.pow(5, 2);15        System.out.println("Circle area: " + area178.53981633974483);16        17        // With static import - cleaner!  //?with18        System.out.println("\nWith static import:");19        double area2→ 78.53981633974483 = PI * pow(5, 2);20        System.out.println("Circle area: " + area278.53981633974483);21        22        System.out.println("\n=== Geometry Calculations ===");23        24        double radius→ 10.0 = 10;25        System.out.println("Radius: " + radius10.0);26        System.out.println("Circumference: " + (2 * PI * radius10.0));27        System.out.println("Area: " + (PI * pow(radius10.0, 2)));28        29        // Pythagorean theorem  //?pythag30        double a = 3, b→ 4.0 = 4;31        double c→ 5.0 = sqrt(pow(a3.0, 2) + pow(b4.0, 2));32        System.out.println("\nPythagorean: sqrt(3² + 4²) = " + c5.0);33        34        // Trigonometry35        double angle→ 0.7853981633974483 = PI / 4;  // 45 degrees36        System.out.println("\nAngle: 45° (π/4 radians)");37        System.out.println("sin(45°) = " + sin(angle0.7853981633974483));38        System.out.println("cos(45°) = " + cos(angle0.7853981633974483));39    }
    output=== Static Import ===
    Without static import:
    Circle area: 78.53981633974483
    
    With static import:
    Circle area: 78.53981633974483
    
    === Geometry Calculations ===
    Radius: 10.0
    Circumference: 62.83185307179586
    Area: 314.1592653589793
    
    Pythagorean: sqrt(3² + 4²) = 5.0
    
    Angle: 45° (π/4 radians)
    sin(45°) = 0.7071067811865475
    cos(45°) = 0.7071067811865476

import static java.lang.Math.*; lets you write sqrt(x) instead of Math.sqrt(x).

Exercise: Singleton.java

Implement the singleton pattern using static