I am creating a program with a clase
call Account with the characteristics:
String Usuario;
double Cantidad;
I've already created the get
and 's set
for them, but now I have to implement an método
Enter that adds a quantity to the quantity, this is what I have of that one método
so far:
public double ingresar(double ingreso) {
int fondo;
getCantidad();
fondo = (int) (getCantidad() + ingreso);
return fondo;
}
In this getAmount it returns the amount that the user has entered, then it tries to add the income, which is one variable
of the type double
that we pass as an argument. The call to método
is this:
System.out.println("Indique la cantidad que desea ingresar");
double ingreso = sc.nextDouble();
ingresar(ingreso);
It tells me what enter is non-static
and that it can't be referenced to one static
, I think. Could someone explain to me what I have wrong? Thank you
There are misconceptions about OOP
Methods can be instance or class (static), because of the way you declare your method, it is instance method which means that to use it you must first create an object and then call the method through that object.
About the method:
you are indicating that it returns a double but background is an int, which you must correct.
Getters and Setters are methods that are used to manipulate the fields of the class when they are private (generally), this prevents other classes from directly accessing the fields, they are used in the same way as other methods.
The following code is an example of how to declare the Account class
Usage from main (for example)
The error
non-static
means that it cannot access yourmétodo
no estático
call enter , since this is not static like themétodo
one from which you are sending it to call, which I imagine is thepublic static void main
, if you look closely, it indicates the reserved wordstatic
, what it wants say that thatfunción
and allfunciones
that arestatic
belong to the class that contains them, therefore, if you want to call onefunción no estática
from another that isestática
, you must create oneinstancia
of the functionno estática
to be able to access it.In another easier and faster way, it is that you make your
función
enter static , in such a way that it looks like this:This way it would no longer be necessary to create the
instancia
and you could call yourfunción
directly.