Question

I referred this page for understanding about constructors in Java: http://www.javatpoint.com/constructor

So, I pretty much got it as in my own words:

constructor has no return value and is used to construct an object (fill with data) while creating it with the arguments supplied

What is the purpose of using constructor? Why can't the class just take care of this task by itself?

What I meant was use arguments in class definition directly instead of another definition for constructor that takes arguments inside a class.

EDIT 1: I was thinking something like this:

class Car(String theCountry) {
    String country;
    country = theCountry;
    String display() {
        return country;
    }
}

instead of this:

class Car {
    String country;
    Car(String theCountry) {
        country = theCountry;
    }
    String display() {
        return country;
    }
}

and the object initialization could still be the same: Car volvo = new Car("Sweden");

Was it helpful?

Solution

Your example

class Car(String theCountry) {
    String country;
    country = theCountry;
    String display() {
        return country;
    }
}

has several flaws that "real" constructors dont.

You can only have one constructor. Constructor overloading has its very good uses and should be possible.

Every variable is part of the class. Suppose you need to create a temporary variable in your constructor like this:

class Country(String country) {
    String CountryCode;
    CountryCode = Locales.GetCode(country);
}

class Car(String theCountry) {
    String country;
    Country tmpCountry = Country(theCountry);
    country = tmpCountry.CountryCode;
    String display() {
        return country;
    }
}

Then your Car class suddenly has two fields: country and tmpCountry, which you probably don't want.

OTHER TIPS

What is the purpose of using constructor? Why can't the class just take care of this task by itself? What I meant was use arguments in class definition directly instead of another definition for constructor that takes arguments inside a class.

If you are asking "is there a fundamental language design reason why this feature I want is impossible?" the answer is "no". Scala and F# have something like this, and the feature you desire was proposed for C# 6, though I believe it did not end up shipping. (Do a web search for "C# primary constructor" if you want the details.)

All features have to be thought of, designed, specified, implemented, tested, documented and shipped. If those things don't happen, you don't get the feature. For Java and C#, not all of those things happened, so, no such feature. It's a perfectly sensible feature, it's just going to take work to make it happen, and so far no one has done that work.

There are two ways of answering this question.

  1. If you're really asking "Why do I have to call new MyClass()? Why isn't MyClass() enough, like in Python?", this is largely a syntactical matter. For some reason or other the writers of the grammar for the Java language marked constructor invocations with a keyword. Other language authors have skipped the keyword. The reasons have to do more with all the other expression types that the language grammar contains than with the constructor invocation itself.

  2. But if you're asking "Why do I have to pass arguments at initialization time?", well, you don't have to. You can create a no-args constructor if you want. Constructors with arguments are in the language so that users can define their own classes which require mandatory bits of data which aren't known at class compile time, but only at object instantiation time.

you can overload a constructor, with fixed input in the class you would have to write a class for each. Like this you can get different instances. But you have to write a constructor for each one.

example:

MyClass instance = new MyClass(param1,param2);

MyClass instance2 = new MyClass(param1,param2,param3);

Well the purpose of constructor in java is to initialize the instance of a class. But not why we have constructors.

For example :

public class Car
{
   private int wheels;

   public Car(int wheels)
   {
      setNumber(wheels);
   }

   public void setNumber(int wheels)
   {
      this.wheels = wheels;
   }
}

As in the example above you can initialize instance of a class externally, using c.setWheels(4).

The reason why we are using constructor is because constructor does that, but in a way that is safe.

You can refer example below -

https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

http://www.flowerbrackets.com/learn-constructor-in-java/

http://www.flowerbrackets.com/java-constructor-example/

The java language dictates that a constructur must be used to create an instance of a class. Any programming language has its syntax that every programmer should respect to make his program compile. Constructors are used in many languages other than java like C++, PHP, etc

Licensed under: CC-BY-SA with attribution
scroll top