The task I do on this link: Lesson 2. Methods . And the eclipse cursed that the variable x was out of sight! Explain, please, what the focus is.

package MyPack; public class MyClass { /** * @param args */ public static void main(String[] args) { int[] x = {56, 89, 31, 600, 131, 14, 3}; System.out.println(Matrix()); } static int Matrix() { for(int i = 0; i < x.length; i++) { /*Ρ†ΠΈΠΊΠ» ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅Ρ‚ значиния массива Π½Π° 10 ΠΏΡ€ΠΎΡ†Π΅Π½Ρ‚ΠΎΠ² */ x[i] = (x[i]/100)*10 + x[i]; return x[i]; } } } 

    2 answers 2

    Hello. Because declaring a variable X in a function, this variable will now be available only for the actions INSIDE of this function. In order for it to be available for all functions of a class, you need to declare it in the class itself, and not in the function, that is, preferably after public class MyClass {

    Another way is to pass the variable to the Matrix function. It is necessary that the Matrix function take on a value. Example:

     public class MyClass { /** * @param args */ public static void main(String[] args) { int[] x = {56, 89, 31, 600, 131, 14, 3}; System.out.println(Matrix(x)); } static int Matrix(int[] y) { for(int i = 0; i < y.length; i++) { /*Ρ†ΠΈΠΊΠ» ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅Ρ‚ значСния массива Π½Π° 10 ΠΏΡ€ΠΎΡ†Π΅Π½Ρ‚ΠΎΠ² */ y[i] = (y[i]/100)*10 + y[i]; return y[i]; } } } 

    P.S. Remove, corrected. :)

    • Ahh, I love people with a yumka)) Xy [i] Thank you) - Vikkingg
    • hmm ... something that swears at the method - Vikkingg
    • Well, if I understood correctly, then you need to return after the for loop. In simple terms (I don’t know how to do anything else), the Matrix function does not know that there is a return in the for loop, and it requires you to put another return at the end of the function itself, since it must return the value int in any case. - tigr240172

    x is an automatic variable, that is, the main() method declared on the stack, even if the static x method is still local to main() , it is logical that it is invisible outside of it.

    To make it visible, you must at least declare it in class members.