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

Solution 1:

You can't. Variables defined inside a method are local to that method.

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

Solution 2:

Looks like you're using instance methods instead of static ones.

If you don't want to create an object, you should declare all your methods static, so something like

private static void methodName(Argument args...)

If you want a variable to be accessible by all these methods, you should initialise it outside the methods and to limit its scope, declare it private.

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

Global variables are usually looked down upon (especially for situations like your one) because in a large-scale program they can wreak havoc, so making it private will prevent some problems at the least.

Also, I'll say the usual: You should try to keep your code a bit tidy. Use descriptive class, method and variable names and keep your code neat (with proper indentation, linebreaks etc.) and consistent.

Here's a final (shortened) example of what your code should be like:

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're using an array, you would create the array in the main method and pass it as an argument across each method, but explaining how that works is probably a whole new question on its own so I'll leave it at that.

Solution 3:

Just move the declaration of a to make it a private property of class Test3, like this:

public class Test3 {

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

Solution 4:

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

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

Solution 5:

you can declare a 2-d array pointer as a member of the class. Then declare the size and value inside a member function. In this case you can access it form another function..

See 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);
    } 
}