How to use a variable of one method in another method?

I want to know how to use the variable a[i][j] in the Scores() method to use it in the MD() and sumD() methods in the following code:

In my code, the MD() and sumD() methods cannot get the result.

 public class Test3 { public void Scores() { double[][] a= new double[3][5]; int i,j; for(i=0; i<3; i++ ){ for(j=0; j<5; j++){ a[i][j]= (double) Math.random(); System.out.println("a[" + i + "][" + j + "] = " +a[i][j]); } } } public void MD(){ double[][] b =new double[3][5]; int [] m = new int[5]; int i,j; //double[][] a= new double[3][5]; for(j= 0; j<5; j++) for(i=0 ; i<3 ; i++) { b[i][j]=0.0; if(a[i][j]>0.0) m[j]++; } for(j= 0; j<5; j++){ for(i=0 ; i<3 ; i++) { if(a[i][j] > 0.0){ b[i][j]=a[i][j]*m[j]; System.out.println("b[" + i + "][" + j + "] = " + b[i][j]); } } } } public void sumD(){ int i,j,n; double[] sum= new double[3]; double[] k= new double[3]; //double[][] a= new double[3][5]; for(i=0; i<3; i++){ n=0; sum[i]=0.0; for(j=0; j<5; j++){ if(a[i][j]>0.0){ sum[i] += (a[i][j])*2; n++; } } k[i]=sum[i]/n; System.out.println("k[" + i + "] = " + k[i]); } } public static void main(String[] args){ Test3 print= new Test3(); print.Scores(); print.MD(); print.sumD(); } } 

Thanks in advance.

+4
source share
9 answers

You can not. Variables defined inside a method are local to this method.

If you want to share variables between methods, you need to specify them as member variables of the class. Alternatively, you can pass them from one method to another as arguments (this is not always applicable).

+13
source

It looks like you are using instance methods instead of static ones.

If you do not want to create an object, you must declare all your methods static, so something like

 private static void methodName(Argument args...) 

If you want the variable to be accessible to all of these methods, you must initialize it outside the methods and limit its scope, declare it private.

 private static int[][] array = new int[3][5]; 

Global variables usually look from top to bottom (especially for situations like yours), because in a large-scale program they can be harmful, so making it confidential will at least prevent some problems.

Also, I will say normal: you should try to keep the code a little tidy. Use the descriptive names of classes, methods, and variables and keep your code neat (with proper indentation, line breaks, etc.) and consistent.

Here is the final (abbreviated) example of what your code should be:

 public class Test3 { //Use this array in your methods private static int[][] scores = new int[3][5]; /* Rather than just "Scores" name it so people know what * to expect */ private static void createScores() { //Code... } //Other methods... /* Since you're now using static methods, you don't * have to initialise an object and call its methods. */ public static void main(String[] args){ createScores(); MD(); //Don't know what these do sumD(); //so I'll leave them. } } 

Ideally, since you are using an array, you should create an array in the main method and pass it as an argument to each method, but an explanation of how this works is probably a completely new question, so I will leave it to that.

+5
source

Just move the declaration of a to make it a private property of the Test3 class, for example:

 public class Test3 { private double[][] a; public void Scores() { a= new double[3][5]; int i,j; ...etc... 
+2
source

just enter the class variable [i] [j] as a class, declare it outside Scores() , just below the class name

 public class Test3 { double[][] a= new double[3][5]; public void Scores() { .... } ..... } 
+1
source

you can declare a 2-dimensional pointer to an array as a member of a class. Then declare the size and value inside the member function. In this case, you can access it with another function.

Note the use of T [] [] in this code.

  public class Knapsack { //private static Scanner in; int T[][]; int MaximumVal(int wt[],int val[], int total){ int l=total; int m=val.length; T = new int[m+1][l+1]; for (int i=0; i<=m; i++){ for(int j=0; j<=l; j++){ if(i==0 || j==0){ T[i][j]=0; continue; } if(j-wt[i-1] >= 0){ T[i][j]=Math.max(T[i-1][j], val[i-1]+T[i-1][j-wt[i-1]]); } else{ T[i][j]=T[i-1][j]; } } } return T[m][l]; } void PrintPath(int wt[], int val[]){ int i=T.length-1; int j=T[0].length-1; while(j!=0){ if(i>0){ while(T[i][j]==T[i-1][j]){ i--; } } System.out.print(wt[i-1]+" "); j=j-wt[i-1]; i--; } } //Main Function with test case :: public static void main(String args[]){ int wt[]={1,3,4,5}; int val[]={1,4,5,7}; Knapsack K = new Knapsack(); System.out.print("Enter the total value: "); //in = new Scanner(System.in); //int total = in.nextInt(); int result = K.MaximumVal(wt,val,7); // pass total System.out.println("Total value is "+ result); K.PrintPath(wt,val); } } 
+1
source

Pass argument or make class var

0
source

If you want to use the 2d matrix a, you need to declare it outside of your methods.

 public class Test3 { double[][] a= new double[3][5]; public void Scores() { //double[][] a= new double[3][5]; int i,j; for(i=0; i<3; i++ ){ for(j=0; j<5; j++){ a[i][j]= (double) Math.random(); System.out.println("a[" + i + "][" + j + "] = " +a[i][j]); } } } ....... 

You will see that I moved the declaration (previously inside Scores () and which I commented out) outside the function, and now it is a field of the Test3 class. When you declare a variable inside a function, it is local to that function. It cannot be seen by other functions, etc. If you declare it as a class field, your functions will be able to see it. Take a look at this as it may help. Language /VariableScope.htm "> http://www.java2s.com/Tutorial/Java/0020_Language/VariableScope.htm The term" scope "refers only to the lifetime of the variable and where it can be seen. I hope this helps!

0
source
 public class Test3 { double[][] a=new double[3][5]; public void Scores() { int i,j; for(i=0; i<3; i++ ){ for(j=0; j<5; j++){ a[i][j]= (double) Math.random(); System.out.println("a[" + i + "][" + j + "] = " +a[i][j]); } } } public void MD(){ double[][] b =new double[3][5]; int [] m = new int[5]; int i,j; //double[][] a= new double[3][5]; for(j= 0; j<5; j++) for(i=0 ; i<3 ; i++) { b[i][j]=0.0; if(a[i][j]>0.0) m[j]++; } for(j= 0; j<5; j++){ for(i=0 ; i<3 ; i++) { if(a[i][j] > 0.0){ b[i][j]=a[i][j]*m[j]; System.out.println("b[" + i + "][" + j + "] = " + b[i][j]); } } } } public void sumD(){ int i,j,n; double[] sum= new double[3]; double[] k= new double[3]; //double[][] a= new double[3][5]; for(i=0; i<3; i++){ n=0; sum[i]=0.0; for(j=0; j<5; j++){ if(a[i][j]>0.0){ sum[i] += (a[i][j])*2; n++; } } k[i]=sum[i]/n; System.out.println("k[" + i + "] = " + k[i]); } } public static void main(String[] args){ Test3 print= new Test3(); print.Scores(); print.MD(); print.sumD(); } } 
0
source
  public class Practise { String a; public String getA() { return a; } public void setA(String a) { this.a = a; } void meth1(){ this.setA("asd"); System.out.println(a); } void meth2(){ String b="qwerty"; System.out.println(getA()+" "+b); } public static void main(String[] args) { Practise p= new Practise(); p.meth1(); p.meth2(); } } 
-2
source

Source: https://habr.com/ru/post/1397311/


All Articles