Java Reference ยท Keywords and Statements
Java default keyword
Reference for the Java default keyword: default methods in interfaces, the default label in switch, and default values of annotation elements.
default is a keyword with three unrelated jobs. In an interface, it marks a method that has a body, so implementing classes inherit it without writing it (Java 8). In a switch, it labels the branch that runs when nothing else matches. In an annotation declaration, it supplies the value an element takes when the user leaves it out.
Do not confuse the keyword with the default access level (package-private), which is what you get by writing no modifier at all.
interface Greeter { String name(); default String greet() { return "Hello, " + name(); } // default method } switch (day) { case 1 -> System.out.println("Mon"); default -> System.out.println("other"); // switch default } @interface Retry { int times() default 3; } // annotation default
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
// interface default ReturnType name(params) { body } // switch (old style, statement) default: statements // switch (arrow form, statement or expression) default -> expression-or-block-or-throw // annotation element Type element() default constantValue;
| Use | Rules |
|---|---|
| Default method | Only in interfaces; has a body; implicitly public; cannot be static, abstract or final; cannot override Object methods such as toString() or equals(). |
default label in a switch statement |
Optional; may appear anywhere in a classic switch, but is conventionally last. Runs when no case matches. |
default in a switch expression |
Needed unless the cases are exhaustive (all enum constants, or all permitted subtypes of a sealed type). |
| Annotation element default | Must be a constant expression, enum constant, class literal, annotation, or an array of those. null is not allowed. |
Usage notes
- Why default methods exist: they let library authors add a method to an interface without breaking every existing implementation.
Collection.stream(),List.sort(), andMap.getOrDefault()were added this way. - Conflict resolution: a class wins over an interface (a method inherited from a superclass beats an interface default); a more specific interface beats a less specific one; if two unrelated interfaces both provide a default with the same signature, the class must override and may call
Interface.super.method(). - No state: default methods can call abstract methods of the interface but cannot touch instance fields, because interfaces have none.
- A
defaultbeforecasein an old-style switch still falls through into the next group of statements, unless youbreak. Arrow-form branches never fall through. - Pattern switches over a sealed hierarchy may omit
default. Adding a new permitted subtype then makes the compiler flag the switch, which is often exactly what you want; adefaulthides that signal. defaultwith enums: for a switch statement over an enum withoutdefaultthe compiler does not demand all constants (only switch expressions and pattern switches must be exhaustive). Adefault -> throw new IllegalStateException(...)branch is a common way to catch constants added later.defaultis a reserved word, not contextual: you cannot name a variable or packagedefault.- Not the access level. A member with no modifier is called package-private or "default access" in older texts; see
public,protected,private.
Related
switch,interface, Annotation syntax, Reserved words- Lessons: Interfaces, Switch, Annotations