The object Scanner
located in main
(since it will be used from several methods) does not take the data from the user. He Scanner
doesn't work for me.
public static void main(String[] args) {
jugarPartida();
valoracionPartida();
Scanner sc = new Scanner(System.in);
}
public static void jugarPartida(){
System.out.print("Introduce numero: ");
numero = sc.nextInt();
/*Aquí sucede un juego*/
}
public static void valoracionPartida(){
System.out.print("Introduce numero(0-10): ");
valoracion = sc.nextInt();
/*Aquí agradecerá la valoración*/
}
This is because the identifier
sc
is not declared in the methodsjugarPartida
andvaloracionPartida
therefore will give a compilation error, since the compiler will take it as if it never existed.A possible solution is to add an additional parameter (of type
Scanner
) in the methodsjugarPartida
andvaloracionPartida
, in this way, when invoking these methods, you will be able to pass the reference of the object to which the implicit pointer pointedsc
(the one of the methodmain
).It would be like this:
I also emphasize that the variables were not declared
valoracion
andnumero
, otherwise, there will be a compilation error (because the identifier/name will never be created for the compiler).Another solution is to declare the identifier
sc
as global.The code would look like this:
But if you notice, if we compile from the command line:
javac Program.java
, we will notice that it gave us a compilation error:non-static method cannot be referenced from a static context
What does this error mean?
This means that methods
jugarPartida
andvaloracionPartida
need the reference of the object (in this case of typeProgram
), however the real reason for the error is because no object of type was ever createdProgram
, so how could the static method access the attributesc
if actually the object does not exist? Well the answer would be NO . It's not possible and it doesn't make sense either.Is there a solution without the need to add the keyword
static
to the variablesc
?Well, there is. One way to avoid this error is by adding an additional parameter to the methods
jugarPartida
andvaloracionPartida
, in which that parameter will be of typeProgram
and we would have to instantiate the class in the methodmain
and send that object reference to the method parameter.In code it would be like this:
In this way, it will not give a compilation error because the object was created and the implicit pointer
p
(of typeProgram
) will have the reference of the object and in this way we will be able to access the attributesc
without any problem.However, we notice that this solution makes us instantiate the class when in reality the object of type
Program
will be created only once, therefore it is unnecessary, because we will not have any more object of typeProgram
. So, in order not to create an object for pleasure, we could declare the global variablesc
as static and there we would not need the methods to have a reference to the object of typeProgram
, because the variablesc
would be stored in a static zone (in this memory zone is where static data is recorded that lasts until the program ends) and it is valid to be able to access the content of the variablesc
in a static method, because the variablesc
would not be in an object.With the changes made, we arrive at this code:
The error is that your
Scanner
should be insidejugarPartida()
or as a global and the variablenumero
is not instantiated:So that you can use it in more than one method you must declare it globally:
This way you can use it in all the methods that are inside your class
EDIT:
The variable must be declared
static
, since only then can it be used in the methodsstatic
:to use the scanner class globally, just start it in the main class, that is, in the main class, not inside the main method. We also needed to declare the number and value variables, which in that case would go inside their methods unless you want to use them in other methods, in that case they would also go outside the main method, in the main class. It would look like this:
I hope it helps you, greetings.