
- 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 return type of a 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.
Return type of a constructor
- A constructor doesn’t have any return type.
- The data type of the value retuned by a method may vary, return type of a method indicates this value.
- A constructor doesn’t return any values explicitly, it returns the instance of the class to which it belongs.
Example
Following is an example of a constructor in java −
public class Sample{ public Sample(){ System.out.println("Hello how are you"); } public Sample(String data){ System.out.println(data); } public static void main(String args[]){ Sample obj = new Sample("Tutorialspoint"); } }
Output
Tutorialspoint
Example
class Student{ Integer age; Student(Integer age){ this.age = age; } public void display() { System.out.println("Value of age: "+this.age); } } public class GenericsExample { public static void main(String args[]) { Student std = new Student(25); std.display(); } }
Output
Value of age: 25
- Related Articles
- Does a constructor have a return type in Java?
- Java constructor return a value but, what?
- What is covariant return type in Java?
- What is the purpose of a constructor in java?
- What is a constructor 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 use of constructor in Java?
- What is the return type of a “count” query against MySQL using Java JDBC?
- Does constructor return any value in Java?
- What is the purpose of private constructor in Java?
- What is the use of parametrized constructor in Java?
- Importance of return type in Java?
- What is return type of db.collection.find() in MongoDB?
- What is constructor overloading in Java?

Advertisements