class Test{
public Test(){
System.out.println("I am here");
return;// not getting error
}
public static void main(String[] args){
Test test = new Test();
}
}
I am java beginner. My tutor told me that constructor returns an object and showed me the above example. is it really happening because method is not returning any value but the return;
is not getting an error.
No, constructor does not return any value. While declaring a constructor you will not have anything like return type. In general, Constructor is implicitly called at the time of instantiation. And it is not a method, its sole purpose is to initialize the instance variables.
Constructor in java is used to create the instance of the class. Constructors are almost similar to methods except for two things - its name is the same as the class name and it has no return type. Sometimes constructors are also referred to as special methods to initialize an object.
No, constructor does not have any return type in Java. Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. Mostly it is used to instantiate the instance variables of a class.
So the reason the constructor doesn't return a value is because it's not called directly by your code, it's called by the memory allocation and object initialization code in the runtime. Its return value (if it actually has one when compiled down to machine code) is opaque to the user - therefore, you can't specify it.
No, a constructor initialises an object that's already been created. It doesn't return an object. Your tutor is wrong.
You don't need to write return;
inside a constructor, unless you're returning before the end of the code.
this line of code
return;
dose not mean return value but it just end the execution of contractor and since it's in the last line so it's not necessarily
situation where you use return
in constructor
class Test
{
private int a;
public Test(int a)
{
System.out.println("I am here");
if(a>10)
{
System.out.println("I'm Executed but not the rest of code");
return;
}
System.out.println("I'm the last line of constructor");
}
...
}
return
in constructor
its just like return
in void
method
No. The role of constructor is to initialize the state of the object.
new
keyword is responsible to create an object in the heap. In below example, using new
keyword you are creating an object and then you are pointing that object with type Test
variable name test
.
Test test = new Test();
No, that is not true at all
A constructor in Java is a block of code similar to a method that’s called when an instance of an object is created. Here are the key differences between a constructor and a method:
A constructor doesn’t have a return type.
The name of the constructor must be the same as the name of the class.
Unlike methods, constructors are not considered members of a class.
A constructor is called automatically when a new instance of an object is created.
Example:
class Bike1{
Bike1(){
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike1 b=new Bike1();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With