Predefined & static methods — cheat sheet
From: fundamentals/10-predefined-methods.md, fundamentals/11-static.md. Detailed tables live in the fundamentals files — this is the conceptual summary worth being able to explain out loud.
static
- Marks a field/method as belonging to the class, not to individual instances — one shared copy, not one per object.
- A static method is called on the class itself (
ClassName.method()), can't usethis, and can't access non-static (instance) fields directly. - Common uses seen here: a shared counter across all instances of a class (
private static int count); utility classes likeCollections/Maththat are never instantiated, just called via the class name. - Course guidance: avoid reaching for
staticin your own classes until you have more experience — it's easy to end up with shared mutable state across every instance (the same bug pattern as [[java-oop/02-mutability]]) and it tends to make code harder to test (can't substitute/mock a static call the way you can an injected instance).
String methods (immutable — every method returns a new string)
length(), isEmpty(), equals/equalsIgnoreCase, startsWith/contains/endsWith, toUpperCase/toLowerCase, trim(), substring(start, end) (end exclusive), replaceAll(target, replacement), split(separator) (returns String[], wrap with List.of(...) for a List).
Random
Create one instance, reuse it. nextInt() (any int), nextInt(n) (0 to n-1), nextDouble() (0.0-1.0), nextBoolean(). Ranged roll pattern: min + random.nextInt((max - min) + 1).
Object methods every class inherits
toString()— string representation; default is unhelpful (class name + memory address) unless overridden.equals(other)— default is identity/reference comparison unless overridden.compareTo(other)— ordering;0/negative/positive for equal/smaller/bigger.- Because every class is an
Object, generic code (collections, comparisons) can work across arbitrary types by only knowing aboutObject's methods.
Collections / Math static utilities
Collections.reverse/sort/shuffle(list)— mutate the list in place, so it must be mutable (e.g.ArrayList, notList.of(...)); copy first withnew ArrayList<>(immutableList)if needed.Math.ceil/Math.floor(round, returndouble),Math.abs(works forintordouble).
Type conversion
Integer.valueOf(String)/Double.valueOf(String)— parse a numeric string; throw if the input isn't valid for that type.String.valueOf(Object)— converts anything to a string; never throws.