Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it true that constructor returns an object?

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.

like image 868
Avinash Kumar Avatar asked Aug 12 '17 04:08

Avinash Kumar


People also ask

Does a constructor return an object?

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.

Does the constructor return anything at all?

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.

Does constructor have return type?

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.

Why do constructors not return values?

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.


4 Answers

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.

like image 92
Dawood ibn Kareem Avatar answered Oct 17 '22 22:10

Dawood ibn Kareem


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

like image 36
Ali Faris Avatar answered Oct 17 '22 21:10

Ali Faris


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();
like image 2
Ravi Avatar answered Oct 17 '22 21:10

Ravi


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();  
    }  
} 
like image 2
Kashif Faraz Shamsi Avatar answered Oct 17 '22 21:10

Kashif Faraz Shamsi