I
I
Ilya Balabanov2014-12-20 14:46:05
Java
Ilya Balabanov, 2014-12-20 14:46:05

Is it possible to access a variable outside of a function?

Hello! I'm just learning the Java language and there was a problem when writing the code. Is there any way I can refer to an int variable outside of a function/method ? Thanks in advance for the help
actually the code
static int Current(int [][] m) {
Sys.pln(" Move from which cell?");
int i = Sys.readInt();
int j = Sys.readInt();
int current = m[i][j];
return current;
}
static int Target(int [][] m) {
Sys.pln("Which one?");
int i = Sys.readInt();
int j = Sys.readInt();
int target = m[i][j];
return target;
}
public void doMotion(int[][] m){
current=target; - error
}
This is something like working with an 8x8 matrix to create a checkers board, I'm trying to make a move with a checker

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Armenian Radio, 2014-12-20
@gbg

Global variables do more harm than good. Fix the architecture.

M
Max, 2014-12-20
@mbelskiy

If the method is not static, you can refer to any class variable.
If static - then only to a static variable.

O
Oleg Gamega, 2014-12-20
@gadfi

Handles would be torn off for this, cases when it is justified nothing at all ... getters, setters are our everything)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question