Methods VS Constructors in Java

Methods vs Constructors in Java

The important difference between constructors and methods is that constructors initialize objects that are being created with the new operator, while methods perform operations on objects that already exist.

Constructors can't be called directly; they are called implicitly when the new keyword creates an object. Methods can be called directly on an object that has already been created with new.

The definitions of constructors and methods look similar in code. They can take parameters, they can have modifiers (e.g. public), and they have method bodies in braces.

Constructors must be named with the same name as the class name. They can't return anything, even void (the object itself is the implicit return).

Methods must be declared to return something, although it can be void.

Can we say Constructors are Methods in Java?

Can we say Contructors are Methods in Java?

If you're new to Java and trying to grasp the concept for the first time, you can think of constructors as factory methods. (Like in Python for instance where a constructor just a method called __init__.) You should however move on quickly and understand that there are many differences. To name a few:

  • A constructor does not have a return type.
  • It has special obligations when it comes to initializing final member variables (a method can't even assign to final members).
  • It's static in the sense that you can invoke it without a callee, but it's non-static in the sense that you have a this reference.
  • It's invoked with special keyword, new, and has a special bytecode, invokespecial, while instance methods are called by obj.method() which typically compiles to the invokevirtual bytecode.
  • It must invoke super constructors unless there's a no-arg constructor in super class.
  • They are never inherited and can not be overridden.

Java Methods vs constructors parameter comparison

A Java method is use to perform some action which is also know as Function. Where you can pass parameter. They must have return type.

A Constructor is special method use to initialise the object . Constructor must not have return type. Constuctor name must be same as class name.

Setter methods or constructors

You should use the constructor approach, when you want to create a new instance of the object, with the values already populated(a ready to use object with value populated). This way you need not explicitly call the setter methods for each field in the object to populate them.

You set the value using a setter approach, when you want to change the value of a field, after the object has been created.

For example:-

MyObject obj1 = new MyObject("setSomeStringInMyObject"); // Constructor approach
// Yippy, I can just use my obj1, as the values are already populated
// But even after this I can change the value
obj1.setSomeString("IWantANewValue"); // Value changed using setter, if required.
..
MyObject obj2 = new MyObject();
obj2.setSomeString("setSomeStringNow"); // Setter approach
// values weren't populated - I had to do that. Sad :(

And as Axel mentioned, if you want to create immutable objects, you cannot use setter-methods approach. I won't say everything has to be initialised in the constructor because different approaches exist, like lazy-evaluation which can be used even with immutable objects.

Difference between constructors and methods

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.

Difference between a method with void and a constructor

Constructors are methods that belong to a class that are associated with its creation. When you declare an object using Object a = new Object(); This is where constructors are invoked.

You should use constructors to organize any data that you'll need for the rest of the class. For example, if you are making a Time class, the Time constructor might get the current time and set it in a variable to use later.

Other methods are simply that. They are the methods that do some calculations or work for the class. For example, you might have a method that accepts a date and returns the days between the date entered, and the current date.

Java - method vs constructor arguments

It depends on the wider context of the object.

If your object is highly cohesive, i.e. it has a narrow scope in its purpose which is primarily to read from a particular file, then it should be defined in the constructor. In terms of OO design, high cohesion is generally a good thing - so I'd generally favour this option. If your application is multi-threaded and thus your object needs to be thread safe, then I'd definitely lean towards this - I'd argue this approach makes it easier to build granular, immutable objects which is a great help when trying to avoid race hazards.

If your object is responsible for many other tasks, you don't really need to worry about concurrency, and it doesn't really make sense for the file to be included as part of the state of the actual object, then the method that takes the parameter would arguably be the best choice.



Related Topics



Leave a reply



Submit