简体   繁体   中英

Consequences of no constructor in java OOP

my question is pretty much in the title. I know that I will be given a default constructor if none is explicitly defined, but if there were no constructors in my class at all, what could I not do? My lecturer has been hounding me for days and no matter how many articles I read I just can't find the answer to this.It is a fundamental of Java that is affected if you do not have any constructor whatsoever.

I apologise for maybe a trivial question but I'm getting desperate

if there were no constructors in my class at all, what could I not do

  1. You cannot initialize your object's member variables using constructor parameters. In order to do that, you would need to use setX methods for nonpublic (protected, private) variables, or use dot notation if they are public.
  2. As a side effect of 1, you cannot have uninitialized final member variables

Having no constructor does not prevent you from instantiating the object as the default constructor will be made. You cannot just "remove" the default constructor to literally have no constructor.

The short answer is: No-constructor in concrete types and Java are two concepts that cannot live next to each other:

No concrete type in Java exists without constructor. That is, the Java compiler won't let you define a class without a constructor (implicit or otherwise).

No object that is created is created without a constructor (though there are a myriad ways of invoking constructors).

So: What would happen if you had no constructor at all? You wouldn't have any runnable Java code.

There are quite a few situations in which you don't need to create an explicit constructor. For example, you can instantiate all default values in the declaration of members and then use setters for creating values.

class SoccerTeam {
    private final List<Person> members = new ArrayList<>();
    private double budget = 0.0;

    public void setBudget(double budget) {
        this.budget = budget;
    }

    public void addMember(Person member) {
        members.add(member);
        budget -= member.getSalary();
    }

    public boolean isBroke() {
        return budget < 0;
    }
}

this could be used as you would expect:

SoccerTeam acMilan = new SoccerTeam();
acMilan.setBudget(1000000);
acMilan.addMember(messi);
assertTrue(acMilan.isBroke());

That's a perfectly acceptable and usable class with no constructor defined.

To answer your lecturer's question, one of the key limitations is that it makes it difficult to define immutable objects . These are objects that are given state when they are constructed and then never change. There are lots of advantages to using immutable objects whenever possible (particular when your code relies on concurrency) but they generally rely on setting final instance variables using values passed into the constructor.

也许他指的是以下事实:没有构造函数,JVM不知道它必须为对象保留内存?

Without a constructor, you cannot create new instance of a class. However, all Java classes have a constructor. If you don't write a constructor yourself, the compiler generates a default constructor for you. So the question of your instructor is comparable to: what would happen if the earth were flat? The answer is: I don't care, it is not so.

However, you can specify a constructor which is not visible, for example:

public class MyClass {
    private MyClass() {
        // ...
    }
}

Since the class has a constructor, the compiler will not generate one, and thus, the only constructor of the class is not visible to any other class. If the class does not provide any static methods which create instances of the class, no instances of the class can be created, and thus only the static methods and variables of the class can be used. In fact, this is a standard pattern to prevent anyone from instantiating a class (for example if the class only provides a set of static utility methods).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM