
- 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 many types of constructors are there 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
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
No-arg constructor
A no-arg constructor doesn’t accepts any parameters, it instantiates the class variables with their respective default values (i.e. null for objects, 0.0 for float and double, false for Boolean, 0 for byte, short, int and, long).
Example
public class Test { int num; String data; float flt; Test(){ this.num = 100; this.data = "test"; this.flt = 125.33f; } public static void main(String args[]){ Test obj = new Test(); System.out.println(obj.num); System.out.println(obj.data); System.out.println(obj.flt); } }
Output
100 test 125.33
- Related Articles
- How many types of wool are there?
- How many types of motion are there ?
- How many types of fibres are there?
- How many types of forces are there ?
- How many types of inheritance are there in Python?
- How many different types of eclipses are there?
- How many types of JDBC Drivers are there?
- How many types of Third-Party Risks are there?
- How many types of Result Sets are there in JDBC What are they?
- How many non-access modifiers are there in Java?
- How many types of anonymous inner classes are defined in Java?
- How many joints are there?
- How many Vitamins are there?
- How many integers are there?
- How many Blockchains are there?
