Skip to content

Commit a7b0e71

Browse files
committed
feat(constructors): add example demonstrating parameterized constructor and instance initialization
What - Introduced `Constructors` class with two instance attributes: - `label` (String) to represent a descriptive identifier. - `number` (double) to represent a numeric value. - Implemented a parameterized constructor: - Accepts `valueOfLabel` and `valueOfNumber` as arguments. - Initializes `label` and `number` fields at the time of object creation. - Added `main` method: - Creates an instance `thing = new Constructors("HIS", 1.2)`. - Prints the assigned label using `System.out.println`. Why - Demonstrates how Java constructors provide a way to initialize objects at creation. - Reinforces the distinction between **constructors** and **methods**: - Constructor has the same name as the class, no return type. - Automatically invoked when an object is created. - Encourages writing constructors for encapsulating initialization logic instead of using setter methods later. Logic 1. Fields declared: `label` and `number`. 2. Constructor `Constructors(String, double)`: - Binds the incoming arguments to the instance attributes. 3. Inside `main`: - `new Constructors("HIS", 1.2)` allocates memory and calls the constructor. - Constructor assigns `"HIS"` to `label` and `1.2` to `number`. - `thing.label` prints `"HIS"`. Real-life applications - Parameterized constructors are widely used to ensure **valid state** of objects at creation: - Example: Creating `User(name, email)` ensures a user always has required info. - Example: Creating `BankAccount(accountNo, balance)` enforces initialization rules. - Reduces risk of uninitialized objects and enforces **immutability** when combined with `final` fields. Notes - If no constructor is defined, Java provides a default no-arg constructor. - Once a parameterized constructor is defined, the default constructor is no longer provided unless explicitly added. - Good practice: use constructors for mandatory fields, setters for optional ones. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 1c71d02 commit a7b0e71

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
public class Constructors
2-
{
3-
//Instance attributes//
1+
public class Constructors {
2+
//Instance attributes
43
public String label;
54
public double number;
65

7-
//Constructor//
8-
public Constructors(String valueOfLabel, double valueOfNumber)
9-
{
6+
//Constructor
7+
public Constructors(String valueOfLabel, double valueOfNumber) {
108
label = valueOfLabel;
119
number = valueOfNumber;
1210
}
1311

14-
public static void main(String[] args)
15-
{
12+
public static void main(String[] args) {
1613
//create an object and assign the values at the same time
1714
Constructors thing = new Constructors("HIS", 1.2);
1815

19-
//let's see if it worked
2016
System.out.println("My thing's label is " + thing.label);
2117
}
2218
}

0 commit comments

Comments
 (0)