Constructor Code

Premium Content - Free Preview

We saw that all methods need return types or the word void for no return type. However, there is one exception: constructors. This is because a constructor is called when an object is created, so it 'returns' the Object itself and cannot return anything else.

source code

All about Constructors
A constructor is created by simply writing a method with the same name as the class itself (including the capitalization). The constructor is usually used to set up the values of variables in the object and run any methods that need to be run on creation. Like any method, a constructor can take any number of parameters. As mentioned, a class can have multiple constructors as long as each one has different parameters.

End of Free Content Preview. Please Sign in or Sign up to buy premium content.


Contact Us
Sign in or email us at [email protected]