简体   繁体   中英

Java EE 6 - Why do I need default constructor and how to define optional parameters?

I'm trying to learn Java and I've got everything setup and I'm running. I created a constructor for my first model and went to setup a constructor function that accepts a parameter. I added this function:

public Guest(String name) {
    this.name = name;
    this.signingDate = new Date(System.currentTimeMillis());
}

Netbeans threw an error saying I needed a default constructor. The error went away when I changed it to:

public Guest() {
}

public Guest(String name) {
    this.name = name;
    this.signingDate = new Date(System.currentTimeMillis());
}

I can't imagine that this is how you create optional parameters within Java. So it brings up 2 questions:

  1. Why do I need a default constructor?
  2. How do I create optional parameters?

实体类必须具有无参数的public或protected构造函数。

Another scenario where you'd need no-arg constrcutors is when you have subclasses.

For example,


class Car {

 Engine engine;
 Car(Engine e)
   {
     // do something
   }

 }

And you have a subclass,


class Benz extends Car
 {
   Benz()
   { 
   }
 }

Now you can't instantiate a Benz object. The reason is, when a new Benz() instantiation is attempeted, the parent constructor is first invoked.

ie,


class Benz extends Car
 {
   Benz()
   {
     super(); //implicitly added 
   }
 }

By default, it tries to invoke a no-arg constructor of the super-class. So you'd need one, unless you have an explicit super(arg) call like this:


class Benz extends Car
 {
   Benz()
   {
     super(new Engine()); 
   }
 }

Note: A simple Java rule to remember A no-arg constructor is provided by default as long as you don't have another constructor with argument.

ie

 class Car{} 
is equivalent to
 class Car{ Car() } 

But when you write,

  class Car{ Car(Engine e){} } 
there is no no-arg constructor provided by default. You'll have to explicitly code
  class Car{ Car(){} Car(Engine e)} 
if you needed it.

Yes. Java supports method / constructor overloading.

You don't "need" the default constructor. However, NetBeans is being helpful to you, and informing you that you need one because it assumes you are creating a bean - which requires the option to be created without a constructor requiring parameters - because the calling code wouldn't know what to populate into these.

For "optional" parameters, you can define as many constructors as you would need - with each one omitting one or more of the "optional" parameters.

Why do I need a default constructor?

I think you are trying to call new Guest() and have only one constructor in Guest class that is Guest(String arg) . And that's why it is showing error that you need to create a constructor with no arguments that is default constructor of a class.

How do I create optional parameters?

If you want to make a constructor with 0 more arguments you need to use ... in argument. Ex:

public class Testing {
    public Testing(String... str) {
    }
    public static void main(String[] args) {
        Testing testing = new Testing();
        Testing testing1 = new Testing("1");
        Testing testing2 = new Testing("2","1");
    }
}

If you're using a POJO (Plain Old Java Object) than you need a default constructor only if you create an instance of this object by invoking no-argument constructor like Guest g = new Guest() .
That's pretty obvious - you want to use no-argument constructor - you need to have one.

If you don't specify any constructor, the compiler will generate one (default, no-argument) for you. If you specify at least one constructor by yourself - the compiler won't generate any as it assumes that you know what you're doing.

However, if you're creating a JPA entity (so you have @Entity at the top of your class) than you need a default constructor as it's a JPA requirement .
It's because instances of your class are created by the JPA provider when you fetch them from the database. The JPA provider has to have a way to instantiate your Entity.
If you'd have only parametrized constructor - how the JPA would know what parameters should it pass?

There are no optional parameters in Java like there are in ie PHP world where you can define myMethod($var1, $var2 = null, $var3 = 2); . In Java world we use overloaded methods and varargs .

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