answersLogoWhite

0


Best Answer

Return a value in the constructor is senseless, because a constructor is used to initialize a object instance and not to perform a task or a operation. When we call a constructor we used a sentence like this:

MyClass var = new MyClass();

Then when we execute the above line the constructor return ('create') a new object of the type 'MyClass'. If this call could return an other type, for example an Integer, the constructor is considered a normal method, and if there are not more constructors a empty default constructor for MyClass is defined by the java compiler.

public class MyClass{

// The java compiler will insert a real constructor here

public Integer MyClass(){ //This isn't a constructor, only a simple method

return new Integer(1);

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

The constructor is not a normal java method. It creates and returns an object of its class when invoked.

ABC obj = new ABC();

The above declaration creates a new object of type ABC and assigns it to the variable obj. Since the constructor is a special method we need not specify the return type. It would automatically return an object of the class type. And since it returns an object we cannot specify VOID in the method signature.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why you don't use void in constructor function although it doesn't return any value?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


How constructor is different from normal member function?

A constructor differs from a normal member function in three ways:A constructor never returns a result. The constructor's declaration reflects this by not even declaring the function as "void." The common design hypothesis is that a well-designed constructor cannot fail, other than maybe in an irrecoverable way (such as a fatal running out of memory).A constructor is never called explicitly except with the new operator.Constructors impose further restrictions. For example, they cannot be declared abstract or virtual, and may have visibility requirements. The common design practise is that at least the default constructor is declared public.


What are the similarities between constructor overloading and function overloading?

The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.


What is return data type?

A Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


What is the name of the special member function which is automatically called during creation of each class object?

The constructor. The constructor instantiates the object, and can optionally take parameters and has an optional initialization phase. It has no return type, and has the same name as the class itself. The constructor can be overloaded. It cannot be virtual or constant.

Related questions

True or False A C plus plus class constructor cannot return a function value?

True - A C++ constructor cannot return a value.


How constructor is different from normal member function?

A constructor differs from a normal member function in three ways:A constructor never returns a result. The constructor's declaration reflects this by not even declaring the function as "void." The common design hypothesis is that a well-designed constructor cannot fail, other than maybe in an irrecoverable way (such as a fatal running out of memory).A constructor is never called explicitly except with the new operator.Constructors impose further restrictions. For example, they cannot be declared abstract or virtual, and may have visibility requirements. The common design practise is that at least the default constructor is declared public.


What are the similarities between constructor overloading and function overloading?

The only similarity is that both constructor and function overloads are distinguished by their signature -- the number and type of their arguments. Functions differ in that they also have a return type, which is also part of the signature, whereas constructors have no return type, not even void.


What is return data type?

A Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


Why constructor in Java doesn't have any return type?

The constructor of a Java class is not an ordinary method. Its purpose is not to return any value. The purpose of the constructor is to instantiate the class which it does. Since, the purpose of a constructor is only to instantiate and initialize its class and not anything else, it does not have a return type. All it does is creates an object of that class.


What is the name of the special member function which is automatically called during creation of each class object?

The constructor. The constructor instantiates the object, and can optionally take parameters and has an optional initialization phase. It has no return type, and has the same name as the class itself. The constructor can be overloaded. It cannot be virtual or constant.


What is the difference between consructor and function in java?

Functions and Constructors are similar in many ways. They can have arguments, they can have any amount of code, they can access the class's variables etc. the only difference is that a method in java needs to mandatorily have a return type but a Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


Difference between function and method?

Constructor will be automatically invoked when an object is created whereas method has to be called explicitly. Constructor needs to have the same name as that of the class whereas functions need not be the same. * There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. * There is no return statement in the body of the constructor. * The first line of a constructor must either be a call on another constructor in the same class (using this), or a call on the superclass constructor (using super). If the first line is neither of these, the compiler automatically inserts a call to the parameterless super class constructor.


What is the purpose of the keyword void in a function declaration?

when we declare any function with void,it doesnt return any value


What is the difference between the constructor to and destructor?

Functions and Constructors are similar in many ways. They can have arguments, they can have any amount of code, they can access the class's variables etc. the only difference is that a method in java needs to mandatorily have a return type but a Constructor in java cannot have a return type. It always creates and returns an object of the class for which it is the constructor. You cannot return a value from a constructor explicitly and if you try to do that, the compiler will give an error. The system knows that the purpose of the constructor is to create an object of the class and it will do the same irrespective of whether you declare a return type or not.


How do we invoke a constructor?

In Java, objects are constructed. Every time you make a new object, at least one constructor is invoked. Every class has a constructor, although if you don't create one explicitly, the compiler will build one for you. Ex: class Test { public Test() { } // this is Test's constructor public void Test() { } // this is a badly named, // but legal, method } If you see the example above, you would have realized that the constructor looks a lot like methods. Below are the main distinguishing factors between the constructor and normal methods: 1. The Constructor's name is exactly the same as the name of the class 2. They do not have a return type (Please remember this. A Constructor cannot have a return type as part of the code) 3. Constructors cannot be static, abstract or final


How do you invoke the constructor function in c plus plus?

There is no such thing as a constructor function in C++ (constructors have no return value, not even void, and cannot be called like regular functions). Constructors are invoked rather than called directly, either by declaring a static variable of the class type, or via the C++ new operator.