
- 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 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 the main purpose of a constructor is to initialize the instance variables of a class.
Example
In the following example we are trying to initialize the instance variables of a class using no-arg constructor.
public class Test { int num; String data; Test(){ num = 100; data = "sample"; } public static void main(String args[]){ Test obj = new Test(); System.out.println(obj.num); System.out.println(obj.data); } }
Output
100 sample
Example
In the following example we are trying to initialize the instance variables of a class using parameterized constructor.
import java.util.Scanner; public class Test { int num; String data; Test(int num, String data){ this.num = num; this.data = data; } public static void main(String args[]){ Scanner sc = new Scanner(System.in); System.out.println("Enter a string value: "); String data = sc.nextLine(); System.out.println("Enter an integer value: "); int num = sc.nextInt(); Test obj = new Test(num, data); System.out.println(obj.num); System.out.println(obj.data); } }
Output
Enter a string value: sample Enter an integer value: 1023 1023 sample
- Related Articles
- What is the use of parametrized constructor 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?
- What is the use of setBounds() method in Java?

Advertisements