Programming





What is the difference between Constructor and Method?

What is the difference between Constructor and Method?


by Administrator | 02-Apr-2020
Java

Tags:  Difference  Method  Constructor  Core Java

Share: 


Constructor

Method

A constructor is used to instantiate an Object with some initial values.

A method is a block of code that helps the user achieve specific functionality. The functionality of a method is not pre-defined like a constructor.

A constructor must not have a return type.

A method must have a return type. The method should have a void return type even if it’s not returning anything.

Constructors are invoked implicitly.

Methods are invoked explicitly.

The java compiler provides a default no-arg constructor if you don't explicitly write any.

Methods are user-defined.

A constructor name must be the same as the class name.

Method names may or may not be the same as the class name.

Syntax

[Modifier] <Constructor-Name> ([<Parameters>]) [Throws] {
    //Statements…
}

Syntax

[Modifier] <Return-Type> <Method-Name> ([<Parameters>]) [Throws] {
    //Statements…
}

The modifier of the default no-arg constructor will be same as the class

There is no such rule for methods.


Comments:


There are no comments.

Enter a new Comment:










Copyright Šprgrmmng. All rights reserved.