The Gateway to Computer Science Excellence
0 votes
why did constructor's name is same as class name and it has no return type
in Object Oriented Programming by Junior (853 points) | 69 views
Constructors are used for object instantiation. It is like initializing the object created with the given values. You don't expect it to return anything. It just sets values (does groundwork specified in the constructor). It returns nothing. Not even void

Constructor's name is same as the class name so that the compiler can distinguish it from other functions in that class.

agreed with @gauravkc just would like to rephrase "initializing the object created with the given values" as initializing the fields of the object with given values, after a constructor is invoked a space is created  in memory for the object and initializes its fields

Please log in or register to answer this question.

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,291 answers
104,889 users