Methods & Scope
Static Methods
Class-Level Functions
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.
public class UtilityMethods {
public static void main(String[] args) {
System.out.println("=== Static Utility Methods ===\n");
// No object needed - call on class!
int squareInput = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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 = ;
int cubeInput = ;
int evenInput = ;
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));
}
}
static methods are called on the class: MathUtils.add(a, b).
Factory methods
Static methods that create and return objects.
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 + ")";
}
}
Factory methods can have descriptive names and control object creation.
Static counter across instances
Track data shared by all instances of a class.
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;
}
}
Static variables are shared. Static methods can access static variables.
Static vs instance comparison
Understand when to use static vs instance methods.
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;
}
}
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.
// 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));
}
}
import static java.lang.Math.*; lets you write sqrt(x) instead of Math.sqrt(x).
Exercise: Singleton.java
Implement the singleton pattern using static