Reputation: 31
Does the constructors in JAVA returns any value?
Since we knew that the constructor doesn't have any return type.
But will the constructor in java return any value to the object i.e, the current class instance?
if yes, could you please explain me the reason..!!
Thanks in advance...!!
Consider the following example and please explain me whether the constructors return any value.
class Constructors
{
public int id;
public String name;
Constructors() //default constructor(no-args)
{
id=3913;
name="Saran";
}
Constructors(int idNew,String nameNew) //parameterized constructor
{
id=idNew;
name=nameNew;
}
void display()
{
System.out.println("ID : "+id);
System.out.println("Name : "+name);
}
public static void main(String args[])
{
Constructors s1=new Constructors();
Constructors s2=new Constructors(123,"teja");
s1.display();
s2.display();
}
}
Upvotes: 1
Views: 2912
Reputation: 117
The main purpose of constructor is to initialize the instance variable. constructor does not return any values because it has no return type. when we create a new object constructor invokes automatically and particular instance variables got stored in heap area.
prototype of constructor is
access-modifier class-name(){}
Upvotes: 0
Reputation: 21
No. Java constructor cannot return a value. If required, just create a method which calls the required constructor and returns the required value. See the example below.
public class Tester {
public Tester(){}
public static Tester getInstance(){
Tester tester = new Tester();
return tester;
}
}
Upvotes: 2
Reputation:
Contructors are used to initialize object of class. With constructor nothing is returned but value is assigned to class properties or variables.
Upvotes: 0
Reputation: 131324
TLDR
The constructor is indeed a void
method but its invocation produces a exploitable result that may used or stored in a variable.
So saying that a constructor returns something is finally a misuse of language but as MyClass myClass = new MyClass()
is the very classic way to create an object, we can understand why this shortcut this misuse of language is used.
On the one hand, from the JLS - Chapter 2. The Structure of the Java Virtual Machine - 2.9. Special Methods, you can read that the constructor is a void method (that is without returned type) :
A class or interface has at most one class or interface initialization method and is initialized (§5.5) by invoking that method. The initialization method of a class or interface has the special name , takes no arguments, and is void (§4.3.3).
But on the other hand, from the JLS - 12.4.2. Chapter 12. Execution - Detailed Initialization Procedure, you can read that the constructor returns a reference to the new created object :
Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:
I will try to check it by using a simple class with an empty no arg constructor, a void method that creates a instance by a direct call of the constructor and another that creates the instance with the new
operator :
package init;
public class MyClass {
public void createMyClassWithConstructor() {
MyClass myClass = new MyClass();
}
public MyClass createMyClassWithAMethod() {
MyClass myClass = new MyClass();
return myClass;
}
}
Here is the disassembled code :
Compiled from "MyClass.java"
public class init.MyClass {
public init.MyClass();
Code:
0: aload_0
1: invokespecial #8 // Method java/lang/Object."<init>":()V
4: return
public void createMyClassWithConstructor();
Code:
0: new #1 // class init/MyClass
3: dup
4: invokespecial #15 // Method "<init>":()V
7: astore_1
8: return
public init.MyClass createMyClassWithAMethod();
Code:
0: new #1 // class init/MyClass
3: dup
4: invokespecial #15 // Method "<init>":()V
7: astore_1
8: aload_1
9: areturn
}
We can notice that both void createMyClassWithAMethod()
method and the MyClass()
constructor return nothing: return
is the last executed JVM instruction and return
JVM instruction means a void return from a method.
Only the MyClass getMyClass()
method returns a reference to an object : 7: areturn
.
So, it is right to say that the constructor is a special (invokespecial
) void method.
But in fact even if its at level of its API the constructor is a void method, we can also notice in the disassembled code that the JVM assigns the reference created by the constructor to a local variable in thecreateMyClassWithAMethod()
:
4: invokespecial #15 // Method "<init>":()V is invoked on the object and
// the result is put on the stack.
7: astore_1 // stores the reference into a local variable 1.
So, even if semantically the constructor invocation returns nothing, it produces a result (or an output) that may be exploited to value a variable.
Upvotes: 2
Reputation: 48258
looks like they dont return something, but they return the created instance of the class
A constructor returns a new instance of the class it belongs to, even if it doesn't have an explicit return statement.
imagine the class Point with the getters getX()
and getY()
when you do
foo = new Point();
the constructor returns a new instance of point.
you can do
foo.getX()
the same way as
new Point().getX()
Upvotes: 0
Reputation: 191
They don't return a value per se, as they don't have any return value. What they do, is that they create an instance of an object.
The keyword new
when creating an object is what "returns" the newly created object in a way.
So, the constructor itself is just here to set the values of an object.
Upvotes: 2
Reputation: 2968
The constructor is like a special method which constructs and returns an instance of the class.
Constructors s2=new Constructors(123,"teja");
// Returns an instance of the class Constructors, initialized with 123, "teja"
// and stores it in variable "s2"
Constructors s3=new Constructors(321,"ajet");
// Returns an instance of the class Constructors, initialized with 321, "ajet"
// and stores it in variable "s3"
Upvotes: -1