DEV Community

ViGnEsH
ViGnEsH

Posted on

Constructor

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)

  1. Called automatically when the object is created
  2. No return type required
  3. Should be the same as the class name
  4. constructor job // initializing object-specific values
  5. All classes have a default constructor (jvm => created) ii. no argument constructor ( we are created)

Output


Top comments (0)