mergesort
mergesort

Reputation: 5187

Does the Java constructor return an object or an object's ID when called?

I want to know if a constructor in Java returns something. I know there is no return value like '5' or "Hello World." But if we are assigning a variable to it:

People person = new People();

Then wouldn't it logically make sense for the object or ID to be returned? Is the reference in memory where the object is stored assigned to people?

I am just thinking about this now because I am learning python, and want to connect the __new__ method to a constructor and then __init__ to the body of the constructor (i.e. the initial values). My professor keeps telling me __new__ doesn't exist, so I am hoping to get an answer to make things clearer.

Upvotes: 1

Views: 907

Answers (5)

charandas
charandas

Reputation: 1

according to me, When the constructor used with 'new' keyword, it allocate the memory space for that object.

For Example: in your code People person = new People();

it automatically allocate the space required for the newly created object 'person' in the memory.

Upvotes: -1

human.js
human.js

Reputation: 1382

The return type of a constructor is the Class itself.
You cannot consider them as void but its guaranteed to allocate memory on the heap and initialize the member fields , and finally returning the address of the newly allocated object which we use as reference variables.

Upvotes: 0

DwB
DwB

Reputation: 38290

Short answer: no

More answer: The thing that is "returning" something is the new operator.

When you create a new instance of a class (via the new operator), you must provide two pieces of information to the JVM: the name of the class to be instantiated and any parameters that are required to construct the new object (constructor parameters).

To allow for this, java requires the following syntax:

Type1 variableName = new Type2(parameter list);

In your example: People person = new People(); People is the type of the reference named person and is the type of the object being instantiated (i.e. new People). The constructor requires no parameters (a no-parameter constructor or default constructor) so the parameter list is "()".

Upvotes: 0

lukastymo
lukastymo

Reputation: 26809

constructor is not "normal" method. And you must use operator new with constructor and then you will have reference to the object, so this is pointer (id) to the place in memory.

here is some explanation

Constructor declarations look like method declarations—except that they use the name of the class and have no return type

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533472

In Java you only have primitives and references to objects as types for fields, parameters and local variables. A reference is a bit like an ID, except it can change as any moment without you needing to know when this has happened.

A reference is closer to the concept of a pointer or object index. ie. it refers to a memory location.

new is definitely a keyword in Java, so saying it doesn't exist isn't very meaningful. You could say it doesn't have a one to one mapping in byte code, except byte code is itself run on a virtual machine and the machine actually run could be rather different anyway. i.e. there isn't much point treating byte code as the way things "really" happen.

Upvotes: 3

Related Questions