开发者

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.


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.


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


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 "()".


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜