Sub topic 7
Declaring and Redefining Methods in JShell
This chapter assumes that you can start a JShell session, evaluate expressions, and declare variables. It focuses on methods: reusable blocks of Java code that receive inputs and return results.
Declaring a method without a class
In a compiled Java program, a method normally belongs to a class. JShell removes that requirement. You can declare a method directly at the prompt.
jshell> int square(int x) {
...> return x * x;
...> }
| created method square(int)
jshell> square(12)
$2 ==> 144
jshell> square(-3)
$3 ==> 9The declaration contains a return type, a method name, parameters, and a body. The return statement sends a value back to the caller. A method whose return type is void performs an action but returns no value.
jshell> void greet(String name) {
...> System.out.println("Hello, " + name);
...> }
| created method greet(String)
jshell> greet("Ada")
Hello, AdaJShell reports the method signature, which includes its name and parameter types. The parameter names do not form part of the signature.
Redefining a method
You can declare a method again with the same signature. JShell modifies the existing method instead of reporting the duplicate-method error that a Java compiler would normally produce.
jshell> int square(int x) { return x * x; }
| created method square(int)
jshell> int square(int x) { return x * x * 1; }
| modified method square(int)Redefinition is useful while experimenting. You can correct a formula, add validation, or change diagnostic output without restarting the session.
Existing variables keep their values, but behavior can change when a method is redefined. Test important callers again after changing a method.
| Input | Result |
|---|---|
| `square(5)` before redefinition | `25` |
| `square(5)` after a changed implementation | Depends on the new body |
A changed method may also make another method fail. JShell reports those errors when you invoke the affected method.
Method dependencies and unresolved declarations
A method can refer to another method that has not been declared yet. JShell records the declaration but marks it as unavailable until the dependency exists.
jshell> int twice(int x) { return half(x) * 4; }
| created method twice(int), however, it cannot be invoked until method half(int) is declared
jshell> int half(int x) { return x / 2; }
| created method half(int)
jshell> twice(10)
$3 ==> 20This lets you build related methods in an order that matches your design rather than Java's source-file order.
Inspecting declared methods
Use /methods to list methods currently in the session. Use /drop to remove a method when you want to replace it cleanly or reduce session state.
jshell> /methods
| int square(int)
| void greet(String)
jshell> /drop square
| dropped method square(int)Exercise
Declare an int cube(int x) method, call it with 4, then redefine it so that it returns x * x * x. What result should the call produce?
Show answer
The method should produce 64. The redefinition should be reported as modifying the method if the original signature is unchanged.