DEV Community

AmalaReegan
AmalaReegan

Posted on

Day 12 Constructor in java:

Constructor in java

    **Constructor will not have any return data type**

    **Constructor is usefull for initializing specific 
variables**

    **A constructor in java is a special method that is used to 
initialize object**

    **The constructor is called when an object of a class is 
created**
Enter fullscreen mode Exit fullscreen mode

Usins constructor
Ex program:

class Dmart
{
    String product_name;
    int price;
    int discount;
public Dmart(String product_name, int price, int discount) //parameterist constructor
{

    this.product_name=product_name; 
    this.price=price;
    this.discount=discount;
}
public static void main(String[] args) //main method
{
    Dmart shop = new Dmart("milkbiscuit",25,5);
    shop.sh();
}
public void sh() //method def or methoid body
{
 System.out.println("product name: "+product_name );
 System.out.println("product price: "+price);
 System.out.println("product discount: "+discount );
}
}

Enter fullscreen mode Exit fullscreen mode

Image description

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video →

Top comments (0)

Quickstart image

Django MongoDB Backend Quickstart! A Step-by-Step Tutorial

Get up and running with the new Django MongoDB Backend Python library! This tutorial covers creating a Django application, connecting it to MongoDB Atlas, performing CRUD operations, and configuring the Django admin for MongoDB.

Watch full video →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, cherished by the supportive DEV Community. Coders of every background are encouraged to bring their perspectives and bolster our collective wisdom.

A sincere “thank you” often brightens someone’s day—share yours in the comments below!

On DEV, the act of sharing knowledge eases our journey and forges stronger community ties. Found value in this? A quick thank-you to the author can make a world of difference.

Okay