Java 我在类的构造过程中构造了一个数组,有没有办法使它成为每个方法都能识别的数组?
在构造函数中,我构建了一个数组,我试图在类中的另一个函数中获取该数组的.length,但它不采用相同的数组,即使它的名称相同,它表示数组为空。您的数组应该是一个实例变量:Java 我在类的构造过程中构造了一个数组,有没有办法使它成为每个方法都能识别的数组?,java,Java,在构造函数中,我构建了一个数组,我试图在类中的另一个函数中获取该数组的.length,但它不采用相同的数组,即使它的名称相同,它表示数组为空。您的数组应该是一个实例变量: public class Example { // Private variables defined outside of a method/constructor can // be used anywhere within the class but not outside of the class
public class Example {
// Private variables defined outside of a method/constructor can
// be used anywhere within the class but not outside of the class
private String[] array;
public Example() {
array = new String[6];
}
public int length() {
return array.length;
}
}
你的密码在哪里?