Skip to content

Commit 1234783

Browse files
committed
feat: Add basic constructor demo with static methods in class Constructor [constructor-basics-static]
🔑 Key: constructor-basics-static 🧠 Logic: - Defined a class named `Constructor` to demonstrate: • ✅ A **no-argument constructor** — prints a message when object is instantiated. • ✅ Two **static methods** (`puton()` and `takeOFF()`) representing actions on a Shirt. - Instantiating an object (`new Constructor()`) triggers the constructor automatically. - Static method `puton()` is called using object (though ideally should be `Constructor.puton()`). 📌 Concepts Demonstrated: - 🧱 **Constructor Rules**: • Same name as class. • No return type, not even `void`. • Used to initialize objects at creation. - 🧼 **Static Methods**: • Belong to class, not to instances. • Can be called without creating object (e.g., `Constructor.puton()`). 🧪 Notes: - Calling `takeOFF()` was commented out but can be used similarly. - The constructor acts like an **object initializer**, even if no values are passed or set. 🧠 Analogy: - Think of constructor as a “birth certificate” that sets up an object during its birth. 📎 Educational Value: - Great minimal template for explaining object lifecycle and class-vs-instance behaviors. Signed-off-by: Somesh diwan <[email protected]>
1 parent 6e0ad9b commit 1234783

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
//Remember - A constructor name should be the same as class name
2-
//and must not have a return type(void).
3-
//Think of constructors as the birth certificate of an object. When you create an object in Java, it needs to be initialized with some initial values or properties.
4-
//Constructors are used to initialize these properties and prepare the object for use.
5-
//Remember - A constructor name should be the same as class name and must not have a return type(void).
6-
7-
//If no constructor is defined in a class, Java provides a default no-argument constructor automatically.
8-
//This default constructor initializes the object but doesn't set any properties, leaving instance variables with their default values (e.g., null for objects, 0 for integers, false for booleans, etc.).
91
public class Constructor {
10-
112
private String color;
123
private char size;
134

14-
//Remember - A constructor name should be the same as class name.
15-
Constructor()
16-
{
5+
//Remember - A constructor name should be the same as a class name.
6+
Constructor() {
177
System.out.println("Inside constructor");
188
}
199

20-
public static void puton()
21-
{
10+
public static void puton() {
2211
System.out.println("Shirt is On!");
2312
}
2413

25-
public static void takeOFF()
26-
{
14+
public static void takeOFF() {
2715
System.out.println("Shirt is off!");
2816
}
29-
public static void main(String[] args)
30-
{
17+
public static void main(String[] args) {
3118
Constructor c = new Constructor();
3219
c.puton();
3320
//c.takeOFF();
3421
}
35-
}
22+
}
23+
24+
/*
25+
Remember - A constructor name should be the same as a class name and must not have a return type(void).
26+
Think of constructors as the birth certificate of an object.
27+
When you create an object in Java, it needs to be initialized with some initial values or properties.
28+
Constructors are used to initialize these properties and prepare the object for use.
29+
30+
Remember - A constructor name should be the same as a class name and must not have a return type(void).
31+
32+
If no constructor is defined in a class, Java provides a default no-argument constructor automatically.
33+
This default constructor initializes the object but doesn't set any properties, leaving instance variables with their
34+
default values (e.g., null for objects, 0 for integers, false for booleans, etc.).
35+
*/

0 commit comments

Comments
 (0)