Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Parameterised constructor in java

Date- Sep 09,2023

3371

In Java, a parameterized constructor is a constructor that accepts one or more parameters when an object of a class is created. These parameters are used to initialize the object's instance variables with specific values.

Here's how you define and use a parameterized constructor:

  1. 1. Define the Constructor: Declare a constructor with parameters inside your class. This constructor will specify what values need to be provided when creating an object of the class.

  2. 2. Use Parameters to Initialize Instance Variables: Inside the constructor, use the parameters to initialize the instance variables.

Here's an example of a class with a parameterized constructor:


In this example, the Person class has a parameterized constructor that takes two parameters: name and age. These parameters are used to initialize the name and age instance variables.

You can create an object of thePerson class and pass values for the constructor parameters like this:

Person person1 = new Person("Alliya", 30);

In this case, person1 is an instance of the Person class with the name set to "Alliya" and the age set to 30.

Parameterized constructors are useful when you want to ensure that objects are created with specific initial values. They allow you to provide initial data when constructing objects, making your classes more flexible and versatile.

package Tutorial_01;

public class Person {
	String name;
    int age;
    
    // Parameterized constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // Other methods and fields can follow...
    
    public static void main(String[] args) {
		// TODO Auto-generated method stub
    	Person person1 = new Person("Alliya", 30); 
    	System.out.println(person1.name); // Outputs Alliya
 		System.out.println(person1.age); // Outputs 30
	   
	}
}

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page