Java中的方法是否可以使用类中前面声明的变量,而不必将它们作为参数传递?

Java中的方法是否可以使用类中前面声明的变量,而不必将它们作为参数传递?,java,Java,例如,这是否有效 public void backtrack() { char d = "d";//initialize a series of characters to represent looked for directions char l = "l"; char t = "t"; char temp = " ";//not sure why i put this here String j = DNA1;// create

例如,这是否有效

public void backtrack()
    {
     char d = "d";//initialize a series of characters to represent looked for directions
     char l = "l";
     char t = "t";
     char temp = " ";//not sure why i put this here

     String j = DNA1;// create two strings to be used for indexing purposes 
     String k = DNA2;

     int x = mat_hold[0].length;//goes to the last row
     int e = mat_hold.length;//goes to the last column
     char[][] mat_hold2 = mat_hold;
     opt_path = mat_hold[x][e];//initialize the beginning of the path to be last element in the array

     while(opt_path != mat_hold2[0][0];)//while the current index does not equal the first element in the array
        {
         if(opt_path = d)// if the element in d was obtained from the diagonal
            {
             DNA_seq1.insert(0,j[x]);//using the matrix location [x][e]take the element from the DNA sequence and insert it into the front of the array we are building
             DNA_seq2.insert(0,k[e]);//ditto
             DNA_align.insert(0,"|");//since they are the same in this case, insert a line to connect them
             opt_path = mat_hold[x-1][e-1]//set the opt path to the diagonal value to be searched
            }//since in this case it was taken from the diagonal, that means both letters were the same and so both letters at the given
            //indexes are inserted into the newly constructed sequence
         if(opt_path = l)
            {
             DNA_seq1.insert(0,"_");
             DNA_seq2.insert(0,k[e]);
             DNA_align.insert(0," ");
             opt_path = mat_hold[x][e-1];
            }
         if(opt_path = t)
            {
             DNA_seq1.insert(0,j[x]);
             DNA_seq2.insert(0,"_");
             DNA_align.insert(0," ");
             opt_path = mat_hold[x-1][e];
            }
        }
    }
其中,DNA1、DNA2、Dna_seq1、Dna_seq2、mat_hold等都在类定义的前面声明和构造。或者我必须将所有这些都写入正在传递的参数中吗?

使用而不是局部变量。诸如此类:

public class YourClassName {

    private static final String DNA_seq2 = null;
    private Object DNA_seq1;
    private Object DNA_align;
    private char[][] mat_hold;

    public void backtrack() {
        char d = 'd';//initialize a series of characters to represent looked for directions
        char l = 'l';
        char t = 't';
        char temp = ' ';//not sure why i put this here
        // Rest of your method...
    }
}
实际上,定义顺序是不相关的。字段在类的任何位置都可用,无论在何处声明:

public class YourClassName {

    public void backtrack() {
        char d = 'd';//initialize a series of characters to represent looked for directions
        char l = 'l';
        char t = 't';
        char temp = ' ';//not sure why i put this here
        // Rest of your method... and it can use DNA_seq2, too, although
        // it is declared below.
    }

    private static final String DNA_seq2 = null;
    private Object DNA_seq1;
    private Object DNA_align;
    private char[][] mat_hold;
}
顺序仅与局部变量相关


但是,如果您发布的代码是您的真实代码,那么这肯定是您遇到的最小问题…

在实例方法中,您同时使用实例字段和静态字段。您为什么不自己尝试一下呢?这不是一个复杂的问题,你可以找到你能做还是不能做。谢谢你,是的,我意识到当我尝试初始化它时,我有很多东西要清理。