Commit 3d0aae8
committed
feat: Add VoidMethods class with displayInfo instance method
WHAT the code does:
- Defines a `VoidMethods` class with two instance variables:
- `boolean value`
- `String name`
- Constructor initializes both fields.
- `displayInfo()` is a **void method** that prints object details (does not return anything).
- `main()` creates an object (`true, "OK!"`) and calls `displayInfo()` to print info.
WHY this matters:
- Demonstrates **void methods** that perform actions instead of returning results.
- Shows how constructors initialize object state.
- Reinforces object-oriented design with instance variables and methods.
HOW it works:
1. `new VoidMethods(true, "OK!")` → assigns `value = true`, `name = "OK!"`.
2. `displayInfo()` → prints:
- `"This object has a true value, and it's name is OK!"`.
Tips & gotchas:
- Void methods are best for **side effects** (printing, updating state), not calculations.
- Fields are package-private → best practice is to declare them `private` and use getters/setters.
- For debugging/printing, overriding `toString()` can be more versatile than a custom display method.
- Method naming (`displayInfo`) is clear, but could be renamed `printDetails` for more specificity.
Use-cases:
- Teaching example for **constructors, instance methods, and void return type**.
- Foundation for objects that report their state.
- Useful for logging or displaying information in object-oriented applications.
- Can be extended with more attributes and different display formats.
Short key: class-void methods-display info.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 5d0d7f4 commit 3d0aae8
1 file changed
+10
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
| 1 | + | |
| 2 | + | |
4 | 3 | | |
5 | 4 | | |
6 | 5 | | |
7 | | - | |
8 | | - | |
9 | | - | |
| 6 | + | |
| 7 | + | |
10 | 8 | | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
14 | | - | |
15 | | - | |
16 | | - | |
| 12 | + | |
| 13 | + | |
17 | 14 | | |
18 | 15 | | |
19 | 16 | | |
20 | | - | |
21 | | - | |
22 | | - | |
| 17 | + | |
23 | 18 | | |
24 | | - | |
| 19 | + | |
| 20 | + | |
25 | 21 | | |
26 | 22 | | |
27 | | - | |
| 23 | + | |
0 commit comments