Parameterised constructor in java | Code2night.com
Code2night
  • Home
  • Blogs
  • Guest Posts
  • Tutorial
  • Post Blog
  • Register
  • Login
  1. Home
  2. Blogpost

Parameterised constructor in java

Date- Sep 09,2023

1652

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
	   
	}
}

Comments

Tags

LinkedinLogin
LinkedinProfile
GetLinkedinProfile
C#
Aspnet
MVC
Linkedin
ITextSharp
Export to Pdf
AspNet Core
AspNet
View to Pdf in Aspnet
Model Validation In ASPNET Core MVC 60
Model Validation
Model Validation In ASPNET Core MVC
Model Validation In ASPNET
Image Compression in AspNet
Compress Image in c#
AspNet MVC
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 | 1210
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

Welcome To Code2night, A common place for sharing your programming knowledge,Blogs and Videos

  • Panipat
  • info@Code2night.com

Links

  • Home
  • Blogs
  • Tutorial
  • Post Blog

Popular Tags

Copyright © 2025 by Code2night. All Rights Reserved

  • Home
  • Blog
  • Login
  • SignUp
  • Contact
  • Terms & Conditions
  • Refund Policy
  • About Us
  • Privacy Policy
  • Json Beautifier
  • Guest Posts