简体   繁体   中英

Java class without default constructor

I know if default constructor isn't declared and my class don't have any other constructor Java generates a default constructor for me automatically. If I declare some constructor, I need declare the default constructor manually.

Why is this case lac4 contains default values in attributes which not have been initilized in constructor and I don't have default constructor ("ataque", "vidaAtual" and "vidaMaxima" contains 0)? I know that Java initializes attributes with default values but for my this occurs only in the default constructor.

public class CartaLacaio {

    private int ID;
    private String nome;
    private int ataque;
    private int vidaAtual;
    private int vidaMaxima;
    private int custoMana;

    public CartaLacaio(int ID, String nome, int ataque, int vida, int mana) {
        this.ID = ID;
        this.nome = nome;
        this.ataque = ataque;
        this.vidaAtual = vida;
        this.vidaMaxima = vida;
        this.custoMana = mana;
    }

    public CartaLacaio(int ID, String nome, int custoMana){
        this.ID = ID;
        this.nome = nome;
        this.custoMana = custoMana;
    }

    public CartaLacaio(CartaLacaio origem){

    }

   //Getters and setters

}

My main

public static void main(String[] args) {
        CartaLacaio lac1 = new CartaLacaio(1, "Frodo Bolseiro", 2, 1, 1);
        CartaLacaio lac2 = new CartaLacaio(2, "Aragorn", 5, 7, 6);
        CartaLacaio lac3 = new CartaLacaio(3, "Legolas", 8, 4, 6);
        CartaLacaio lac4 = new CartaLacaio(4, "Teste nome", 3);
    }

...but for my this occurs only in the default constructor.

No, that happens when the instance is created, before any constructor is called.


Just FWIW, if you had initializers on those fields, eg:

public class Example {
    private int a = 42;

    // ...
}

...the code to set those initializers would be inserted automatically into the beginning of every constructor in the class by the compiler.


I know if default constructor isn't declared and my class don't have any other constructor Java generates a default constructor for me automatically. If I declare some constructor, I need declare the default constructor manually.

You're confusing two separate things.

The default constructor is the constructor the compiler will generate for you if you don't provide any constructors.

A zero-parameters constructor is a constructor that accepts no parameters.

The default constructor is a zero-parameters constructor. If you provide a zero-parameters constructor explicitly, it's not a default constructor. "Default" means just that: The default , if no other is provided.

You are wrong in your statement

I know that Java initializes attributes with default values but for my this occurs only in the default constructor.

Java automatically initializes attributes with default values also for overloaded constructor or a non-default constructor

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