Skip to content

Commit 3d0aae8

Browse files
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

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
1-
public class VoidMethods
2-
{
3-
//Instance variables//
1+
public class VoidMethods {
2+
//Instance variables
43
boolean value;
54
String name;
65

7-
//Constructor//
8-
public VoidMethods (boolean inputValue, String inputName)
9-
{
6+
//Constructor.
7+
public VoidMethods (boolean inputValue, String inputName) {
108
value = inputValue;
119
name = inputName;
1210
}
1311

14-
//Instance method//
15-
public void displayInfo()
16-
{
12+
//Instance method.
13+
public void displayInfo() {
1714
System.out.println("This object has a " + value + " value, and it's name is " + name);
1815
}
1916

20-
public static void main(String[] args)
21-
{
22-
//create the object
17+
public static void main(String[] args) {
2318
VoidMethods exampleObject = new VoidMethods(true, "OK!");
24-
//invoke the displayInfo method
19+
20+
//invoke the object and displayInfo method.
2521
exampleObject.displayInfo();
2622
}
27-
}
23+
}

0 commit comments

Comments
 (0)