+ Start a Discussion
Abhilash DaslalAbhilash Daslal 

Constructor vs method

Hi guys,
Can anyone tell me when a constructor is used and preferred more than a method.
What are the limitations of both method and constructor

mukesh guptamukesh gupta
Hi Abhilash
main difference:-

1.Constructor are used to initialize the state of object,where as method is expose the behaviour of object.
2.Constructor must not have return type where as method must have return type.
3.Constructor name same as the class name where as method may or may not the same class name.
4.Constructor invoke implicitly where as method invoke explicitly.
5.Constructor compiler provide default constructor where as method compiler does't provide.

public class Apple {
    //instance variables
    String type; // macintosh, green, red, ...

     * This is the default constructor that gets called when you use
     * Apple a = new Apple(); which creates an Apple object named a.

    public Apple() {
        // in here you initialize instance variables, and sometimes but rarely
        // do other functionality (at least with basic objects)
        this.type = "macintosh"; // the 'this' keyword refers to 'this' object. so this.type refers to Apple's 'type' instance variable.

     * this is another constructor with a parameter. You can have more than one
     * constructor as long as they have different parameters. It creates an Apple
     * object when called using Apple a = new Apple("someAppleType");
    public Apple(String t) {
        // when the constructor is called (i.e new Apple() ) this code is executed
        this.type = t;

     * methods in a class are functions. They are whatever functionality needed
     * for the object
    public String someAppleRelatedMethod(){
        return "hello, Apple class!";

    public static void main(String[] args) {
        // construct an apple
        Apple a = new Apple("green");
        // 'a' is now an Apple object and has all the methods and
        // variables of the Apple class.
        // To use a method from 'a':
        String temp = a.someAppleRelatedMethod();
        System.out.println("a's type is " + a.type);

Kindly mark my solution as the best answer if it helps you.