
- 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
How to create a parameterized 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.
Parameterized constructors
A parameterized constructor accepts parameters with which you can initialize the instance variables. Using parameterized constructor, you can initialize the class variables dynamically at the time of instantiating the class with distinct values.
Example
public class StudentData { private String name; private int age; public StudentData(String name, int age){ this.name = name; this.age = age; } public StudentData(){ this(null, 0); } public StudentData(String name) { this(name, 0); } public StudentData(int age) { this(null, 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 constructor that accepts both values System.out.println("Display method of constructor that accepts both values: "); new StudentData(name, age).display(); System.out.println(" "); //Calling the constructor that accepts name System.out.println("Display method of constructor that accepts only name: "); new StudentData(name).display(); System.out.println(" "); //Calling the constructor that accepts age System.out.println("Display method of constructor that accepts only age: "); new StudentData(age).display(); System.out.println(" "); //Calling the default constructor System.out.println("Display method of default constructor: "); new StudentData().display(); } }
Output
Enter the name of the student: Krishna Enter the age of the student: 22 Display method of constructor that accepts both values: Name of the Student: Krishna Age of the Student: 22 Display method of constructor that accepts only name: Name of the Student: Krishna Age of the Student: 0 Display method of constructor that accepts only age: Name of the Student: null Age of the Student: 22
- Related Articles
- Java parameterized constructor
- What are the differences between default constructor and parameterized constructor in Java?
- Can we define a parameterized constructor in an abstract class in Java?
- What is a parameterized constructor in C# programs?
- How to create a default constructor in Java?
- How to create a constructor reference for an array in Java?
- What are the rules to create a constructor in java?
- Where and how can I create a private constructor in Java?
- How to declare a constructor in Java?
- Is it possible to create static constructor in java?
- How to call the constructor of a superclass from a constructor in java?
- How to create a List with Constructor in C++ STL
- How to use parameterized SQL query in a JSP?
- How to call Private Constructor in Java
- How to create JavaScript objects using object() constructor?

Advertisements