Thursday 30 March 2017

Difference: constructor and java method

Constructor and Method in java differ as illustrated below:

Name:

Constructor should be named same as the class name in which it is created.

Method may or may not bear the same name as that of the class name.

Return type:

A constructor does not have any return type in java.

A method should always be provided with return type in java.

Purpose:

Purpose of having constructor is to initialize state of an object.

Purpose of method is to expose the behaviour of an object.

Default provision:

A default constructor is always provided by java compiler if a programmar doesn't create any constructor.

Method needs to be created by programmar. Its never provided as default.

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.