0
class Empleado_test3 {
    
    private String nombre = new String();
    private double sueldo;
    private Date altaContrato;
    
    
    // ELABORACIÓN DEL MÉTODO CONSTRUCTOR 1
    public Empleado_test3(String nombre, double sueldo, int agno, int mes, int dia) {
        
        this.nombre = nombre;
        this.sueldo = sueldo;
        GregorianCalendar calendario = new GregorianCalendar(agno, mes-1, dia);
        this.altaContrato = calendario.getTime();
        
    }

    // ELABORACIÓN DEL MÉTODO CONSTRUCTOR 2
    public Empleado_test3(String nombre) {
            
        this.nombre = nombre;
        
    }
    
    // GETTER -- sueldo
    public double getSueldo() {
        return sueldo;
    }

Why I can cast getSueldo() from the principal class? I mean, if I instance a object and I attach only the name of the employee, the class launch the second constructor method and "sueldo" will not be initialized. Normally, if I attempt to use a variable that hasn't have been initialized, Java returns an error. In this case Java assigns the value zero to the variable sueldo.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • You make it much easier for the community to understand your problem if you keep your in English (variable names, class names, method names, comments). – EricSchaefer Mar 11 '21 at 19:04
  • @EricSchaefer ,I think he is trying to call or create an instance from the second constructure so the variable nombre means name , but I think you are right, it will become easier to understand if he uses the variables in English – Sana'a Al-ahdal Mar 11 '21 at 20:40
  • @EricSchaefer ,I think he is trying to call or create an instance from the second constructure so the variable nombre means name , but I think you are right, it will become easier to understand if he uses the variables in English – Sana'a Al-ahdal Mar 11 '21 at 20:40

1 Answers1

1

sueldo is zero, because that is the default value of the double type. Reference types have null as default value. Primitive types like double, int and boolean have their interpretation of zero as default value (0.0, 0, false, respectively).

Learn about the difference of reference types and primitive types.

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
  • Yes of course, my doubt is that if in the main class I declare a variable but do not initialize it, when I print it Java gives me an error (The local variable i may not have been initialized). This way Java assigns it its default value. Thanks a lot for your time :) – José Marín Sánchez Mar 11 '21 at 19:07
  • That's because fields in a class are always initialized to it's default value, while local variables (in a method) are not. – EricSchaefer Mar 11 '21 at 19:09
  • Ok, thanks, now I can understand the behavior. – José Marín Sánchez Mar 11 '21 at 19:11
  • The reason for that is simply that you would end up initialiting fields with meaningless values, because you are rather limited in field initialization. E.g. you can not assign the return value of a method in a field initializer. In a method on the other hand, you can do that. – EricSchaefer Mar 11 '21 at 19:11