Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between constructors and methods

Bellow is an example I found on Tutorials Points, an example of a constructor. I got most of them, but I just don't get why you need a constructor and a method.

public Puppy(String name){
    System.out.println("Passed Name is :" + name ); 
}

My question is, what stops you from doing this instead?

public static void Puppy(String name){
    System.out.println("Passed Name is: "+name);
}

Doesn't these two do the same thing once called?

Here is the full program for reference:

public class Puppy {
    int puppyAge;

    public Puppy(String name) {
        System.out.println("Passed Name is :" + name); 
    }

    public void setAge(int age) {
        puppyAge = age;
    }

    public int getAge() {
        System.out.println("Puppy's age is :" + puppyAge); 
        //what does this return do? since the puppyAge is already printed above.
        return puppyAge;
    }

    public static void main(String []args){
        Puppy myPuppy = new Puppy("tommy");

        myPuppy.setAge(2);
        myPuppy.getAge();

        System.out.println("Variable Value :" + myPuppy.puppyAge); 
    }
}
like image 720
Steven Zhao Avatar asked Nov 09 '14 02:11

Steven Zhao


4 Answers

You did not get the basic concept of an instance, which is fundamental in OOP. If you want a metaphor, let's talk about cars.

I'm pretty sure you know what a car is; you know that it enables you to move from one place to another, that it has 4 wheels and so on. It is a concept, and the actual car you have in your garage is an instance of this concept (<=> class).

A constructor's goal is to create an instance, not to print some text. Without a constructor, you will never be able to call a non-static method of your class. You won't be able to drive the concept of a car, you will need to build a car first.

Just review those notions; you will get nowhere without it.

like image 127
Dici Avatar answered Oct 20 '22 11:10

Dici


I guess your question is why the constructor is created using...

public Puppy(String name)

...instead of...

public static void Puppy(String name)

..., right?

If it is, firstly you should now that a constructor is a special method owned by a class that is called whenever you create a new instance of that class. So if, for example, you have this class...

public class Clazz {
    public Clazz (String s) {
         System.out.println(s);
    }
}

..., then whenever you create a new instance of this class like...

Clazz c = new Clazz("Hello World");

..., the constructor will be called and that method will be executed. (In this example, it would print "Hello World" on the screen.)

That said, you can see that the constructor is a method, but it's a special method called when the class is instantiated. So, regarding your question, the reason why the constructor is right this way instead of a regular method header is to indicate to the compiler that it is a constructor and not a regular method. If you write public void Clazz () instead of public Clazz (), then the compiler will not recognize your constructor, and will think it's just a regular method.

Note that constructors never return anything, so it would not be necessary to use the void keyword. Also, there's no sense in making a constructor static, since the constructor is called when a new object is instantiated.

like image 37
felipeek Avatar answered Oct 20 '22 12:10

felipeek


Constructors ARE methods, they are special types of methods, however. Constructors are methods that don't have a return type and must be named after their enclosing class. So why do we need constructors? well, that's how we can create instances of classes. When you want to create an instance of a certain class, you do so by calling its constructor. The constructor reserves memory for the object and returns a reference to the newly created object.

like image 5
turingcomplete Avatar answered Oct 20 '22 11:10

turingcomplete


Stated in the Java Spec for Constructors:

  • Constructors do not return anything, thus they have no return type.

    In all other respects, the constructor declaration looks just like a method declaration that has no result (§8.4.5).

  • You cannot override a constructor.

    Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding.

  • They cannot access instance variables directly, instead they must use this and super to delegate to the classes' variables.

    An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods or inner classes declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.

like image 5
Mr. Polywhirl Avatar answered Oct 20 '22 11:10

Mr. Polywhirl