I'm learning Java and doing a simple introductory OOP exercise. It gives me an error but I am not able to understand where I am failing, I use Netbeans as IDE. Here the class code:
public class Empleado {
// Decimos qué atributos tendrá nuestra clase
private String nombre;
private String apellido;
private int edad;
private double sueldo;
// Utilizamos los constructores para inicializar dichos atributos
public Empleado(String nombre, String apellido, int edad, double sueldo) {
this.nombre = nombre;
this.apellido = apellido;
this.edad = edad;
this.sueldo = sueldo;
}
//Le meto get y set para poder hacer cosas con dichos atributos
public String getNombre() {
return nombre;
}
public void setNombre() {
this.nombre = nombre;
}
public String getApellido() {
return apellido;
}
public void setApellido() {
this.apellido = apellido;
}
public int getEdad() {
return edad;
}
public void setEdad() {
this.edad = edad;
}
public double getSueldo() {
return sueldo;
}
public void setSueldo() {
this.sueldo = sueldo;
}
/**
*
* @return
*/
@Override
public String toString () {
String texto = "El empleado se llama " + nombre + " " + apellido + " y tiene " + edad + " años y cobra un sueldo de " + sueldo + " euros";
return texto;
}
}
And here the code of the main method:
public class Main {
public static void main(String[] args) {
Empleado trabajador1 = new Empleado ("Pablo", "Fernández", 27, 1050);
Empleado trabajador2 = new Empleado ("César", "Romero", 26, 1300);
System.out.println(trabajador1.toString());
System.out.println(trabajador2.toString());
trabajador1.setNombre("Lucía");
trabajador2.setEdad(23);
It gives me an error in the setters and it does not let me execute, it tells me that
"method setName in class employee cannot be applied to given types; required: no arguments; found: String; reason: actual and formal argument lists differ in lenght"
However, as much as I search the Internet, I can't find the error, since at a syntactic level I think I'm doing it right. Any ideas? Thanks.
The setters are not receiving any parameters:
They should all be something like
The code compiles into those classes because
this.
it's optional, really your setters are equivalent toVeteran Tip: If you're using Eclipse, write your classes like this:
And then press Alt+ Mays+ S. In the menu that appears choose Generate Setters and Getters
Pablo Lozano's answer is excellent, I would also like to add that you can use the library for Lombok. Which generates the setters and getters for you automatically with simple annotations.
An example :
With this, the library automatically generates your setters and getters. Don't forget to add the library to your project or as a maven dependency (which I recommend you use).
I leave you this link that can help you with this library.
I hope you find it useful.
When you define a class you are encapsulating information.
in this case you define private attributes (which are inaccessible, "in theory", from outside the class) //when you master more you can look at what reflection is to understand my "in theory".
the idea of object orientation is that each object tells you how you can interact with it.
In your particular case you define a setter to which you do not pass information to update. Note that making this.variable = parameter is equivalent to making the private attribute public.