
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
What is the use of parametrized constructor in Java?
A constructor is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have same name as their class and, have no return type.
There are two types of constructors parameterized constructors and no-arg constructors a parameterized constructor accepts parameters.
The main purpose of a constructor is to initialize the instance variables of a class. Using a parameterized constructor, you can initialize the instance variables dynamically with the values specified at the time of instantiation.
public class Sample{ Int i; public sample(int i){ this.i = i; } }
Example
In the following example the Student class has two private variables age and, name. From main method we are instantiating the class variables using parameterized constructors −
import java.util.Scanner; public class StudentData { private String name; private int age; //parameterized constructor public StudentData(String name, int age){ this.name =name; this.age = age; } public void display(){ System.out.println("Name of the Student: "+this.name ); System.out.println("Age of the Student: "+this.age ); } public static void main(String args[]) { //Reading values from user Scanner sc = new Scanner(System.in); System.out.println("Enter the name of the student: "); String name = sc.nextLine(); System.out.println("Enter the age of the student: "); int age = sc.nextInt(); System.out.println(" "); //Calling the parameterized constructor new StudentData(name, age).display(); } }
Output
Enter the name of the student: Sundar Enter the age of the student: 20 Name of the Student: Sundar Age of the Student: 20
- Related Articles
- What is the use of constructor in Java?
- What are parametrized constructors in Java?
- What is the purpose of private constructor in Java?
- What is the purpose of a constructor in java?
- What is constructor overloading in Java?
- What is a constructor in Java?
- What is constructor chaining in Java?
- What is the purpose of a default constructor in Java?
- What is the super() construct of a constructor in Java?
- What is the return type of a Constructor in Java?
- What are the constructor references in Java?
- What are the differences between default constructor and parameterized constructor in Java?
- Is constructor inherited in Java?
- What is the difference between getter/setter methods and constructor in Java?
- What is the use of marker interfaces in Java?
