
- 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 are parametrized constructors 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.
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.
Syntax
public class Sample{ Int i; public sample(int i){ this.i = i; } }
Example
public class Test { String val; Test(String val){ this.val = val; } public static void main(String args[]){ Test obj = new Test("test"); System.out.println(obj.val); } }
Output
test
Example
import java.util.Scanner; public class Test { int num; String data; float flt; Test(int num, String data, float flt){ this.num = num; this.data = data; this.flt = flt; } public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter an integer value: "); int num = sc.nextInt(); System.out.println("Enter a string value: "); String data = sc.next(); System.out.println("Enter a floating point value: "); float flt = sc.nextFloat(); Test obj = new Test(num, data, flt); System.out.println(obj.num); System.out.println(obj.data); System.out.println(obj.flt); } }
Output
Enter an integer value: 1024 Enter a string value: test Enter a floating point value: 11.2 1024 test 11.2
- Related Articles
- What are constructors in Java?
- What are Default Constructors in Java?
- What are copy constructors in Java?
- What is the use of parametrized constructor in Java?
- Are Multiple Constructors possible in Java?
- What are constructors in C# programs?
- Constructors in Java\n
- How many types of constructors are there in Java?
- Get all Constructors in Java
- Type of Java constructors
- Can interfaces have constructors in Java?
- Can constructors be inherited in Java?
- Constructors of StringBuffer class in Java.
- Constructors of StringBuilder class in Java.
- Constructors of StringTokenizer class in Java.

Advertisements