July 08 2026
Constructor
A constructor in Java is a special block of code, similar to a method, that is automatically called when an instance (object) of a class is created.
Constructor Java Code
This refers to a global variable
ShopConstr Constructor method class name
main method inside
Classname objectCreatedname1 = new Classname("oil",95,5)
Classname objectCreatedname2 = new Classname("rice",1.500,10)
- Called automatically when the object is created
- No return type required
- Should be the same as the class name
- constructor job // initializing object-specific values
- All classes have a default constructor (jvm => created) ii. no argument constructor ( we are created)


Top comments (0)