cover-img

Method vs Constructor

JavaSeries

30 November, 2023

17

17

1

  • In Java, if we keenly observe a constructor pretty much looks like a method, So let's see the major differences between a constructor and a method.
Point.java
package DevLearn.src;

public class Point {
public int x;
public int y;

// a constructor
public Point(int a, int b) {
x = a;
y = b;
}

// a method
public void setPoint(int a, int b) {
x = a;
y = b;
}
}
  • So as we can see from the example,
    • constructor would always have same name as the class & has no return type. The first letter should always be capitalized
    • Where as the method needs to have the return type.
    • Also the constructor can be called only while initializing the object where as we are free to call a method as many times as we would like

17

17

1

More Articles

Showwcase is a professional tech network with over 0 users from over 150 countries. We assist tech professionals in showcasing their unique skills through dedicated profiles and connect them with top global companies for career opportunities.

© Copyright 2025. Showcase Creators Inc. All rights reserved.