为什么数组上的方法必须是静态的(Java)?

时间:2022-09-25 14:08:24

I don't understand why methods on arrays, in Java, have to be static. Static methods can only be used on static variables right? So this would mean arrays are static variables, variables shared by a class? But what class would this be?

我不明白为什么Java中的数组上的方法必须是静态的。静态方法只能用于静态变量吗?那么这意味着数组是静态变量,类是否由类共享?但这会是什么课?

Can someone help me understand this?

有人能帮我理解吗?

Edit: to be more specific, I am creating methods to act on arrays but if I just write "public int[] expandArr(int[]a, int v)" and I try to use this method in the main method, I get an error saying I can't make a static reference to a non-static method. When I write "public static int[] expandArr(int[]a, int v)" it works then.

编辑:更具体一点,我正在创建处理数组的方法,但如果我只写“public int [] expandArr(int [] a,int v)”并且我尝试在main方法中使用此方法,我得到一个错误,说我无法对非静态方法进行静态引用。当我写“public static int [] expandArr(int [] a,int v)”时它就可以了。

I understand you cannot change the size of an array, the method I wrote makes a new array with increased size and copies the first one into it.

我知道你无法改变数组的大小,我编写的方法会生成一个大小增加的新数组,并将第一个数据复制到其中。

Thank you.

5 个解决方案

#1


2  

You say you tried to write this:

你说你试过写这个:

public int[] expandArr(int[]a, int v)

The thing is, you had to write it in some class, since you can't just have free-floating methods in your program. Therefore, it must operate on a instance of the class. For example:

问题是,你必须在某个类中编写它,因为你不能在程序中使用*浮动方法。因此,它必须在类的实例上运行。例如:

public class MyProgram {

    public int[] expandArr(int[]a, int v) { ... }

    public static void main(String[] args) { ... }

}

expandArr requires an instance of MyProgram, since you didn't declare it to be static. And that has nothing to do with arrays. It would be the same if you wrote

expandArr需要一个MyProgram实例,因为你没有声明它是静态的。这与数组无关。如果你写的话会是一样的

public class MyProgram {

    public String expandString(String s, int v) { ... }

    public static void main(String[] args) { 
         String s = expandString(args[0], 1);  // ILLEGAL
         String s2 = args[0].expandString(1);  // ILLEGAL
    }
}

Although the first parameter of expandString is a String, this actually operates on a MyProgram, and you cannot use expandString without an instance of MyProgram to operate on. Making it static means that you can (the first use of expandString in my example would become legal.)

虽然expandString的第一个参数是一个String,但它实际上是在MyProgram上运行的,并且你不能在没有MyProgram实例的情况下使用expandString。使它成为静态意味着你可以(在我的例子中首次使用expandString将变得合法。)

In general, you can't add methods to a class without modifying the source of that class. If you want to write a new method that does something with objects of a certain class C, and you can't modify class C (perhaps because it's in the Java library or someone else's library), then you'll need to put the method in some other class C2, and most of the time you will need to make the method static since it doesn't involve objects of class C2.

通常,您无法在不修改该类的源的情况下向类添加方法。如果你想编写一个新的方法,用某个类C的对象做某事,你不能修改类C(也许是因为它在Java库或其他人的库中),那么你需要把方法放在在其他一些C2类中,大多数情况下你需要使方法保持静态,因为它不涉及C2类的对象。

#2


1  

You can't call a non-static method from a static method unless you first instantiate the an object of the class.

除非首先实例化类的对象,否则不能从静态方法调用非静态方法。

e.g.

In class Whatever...

在课堂上随便......

public boolean ok() {
    return true;
}

public static void main(String[] args) {
    Whatever w = new Whatever();
    System.out.println(w.ok());
}

#3


1  

You cant call a non static method from a static context. A static method belongs to the class, non static or instance methods are copied to each instance of the class (they each have their own). If I have 10 instances of class A, and class A has a static method, which all of them share, then I try to invoke a non static method in class A from class A's static method, which instance of class A gets its method invoked? The behavior is undefined.

你不能从静态上下文中调用非静态方法。静态方法属于该类,非静态或实例方法被复制到类的每个实例(它们各自都有自己的)。如果我有10个A类实例,并且A类有一个静态方法,它们都共享,那么我尝试从类A的静态方法调用A类中的非静态方法,类A的实例调用它的方法?行为未定义。

The question really has nothing to do with arrays.

这个问题确实与数组无关。

This question is related: Can't call non static method

这个问题是相关的:不能调用非静态方法

#4


0  

I think what you're referring to is the fact that you can't extend an array class and hence can't add a method to it:

我认为你所指的是你不能扩展数组类,因此不能添加一个方法:

// No way to do this
int[] array;
array.myMethod();

This means your only option is to make a static helper method somewhere:

这意味着您唯一的选择是在某处创建一个静态帮助器方法:

// So you have to do this
int[] array;
Utils.myMethod(array);

class Utils {
    static void myMethod(int[] array) {
        ...
    }
}

An example of this is the Arrays class, which has lots of static methods for operating on arrays. Conceptually it would be clearer if these methods could be added to the array classes, but you can't do that in Java (you can in other languages like Javascript).

这方面的一个例子是Arrays类,它有许多用于在数组上操作的静态方法。从概念上讲,如果将这些方法添加到数组类中会更清楚,但是你不能在Java中这样做(你可以在其他语言中使用Javascript)。

#5


0  

That is why we use other classes like the ArrayList to make them grow dynamically in size. At the core of an arraylist you will simply find an array that is renewed in size whenever the class figures out it necessary. If I'm understanding your question right.

这就是为什么我们使用像ArrayList这样的其他类来使它们动态增长。在arraylist的核心,你只需找到一个数组,只要课程有必要,它就会更新。如果我正确理解你的问题。

#1


2  

You say you tried to write this:

你说你试过写这个:

public int[] expandArr(int[]a, int v)

The thing is, you had to write it in some class, since you can't just have free-floating methods in your program. Therefore, it must operate on a instance of the class. For example:

问题是,你必须在某个类中编写它,因为你不能在程序中使用*浮动方法。因此,它必须在类的实例上运行。例如:

public class MyProgram {

    public int[] expandArr(int[]a, int v) { ... }

    public static void main(String[] args) { ... }

}

expandArr requires an instance of MyProgram, since you didn't declare it to be static. And that has nothing to do with arrays. It would be the same if you wrote

expandArr需要一个MyProgram实例,因为你没有声明它是静态的。这与数组无关。如果你写的话会是一样的

public class MyProgram {

    public String expandString(String s, int v) { ... }

    public static void main(String[] args) { 
         String s = expandString(args[0], 1);  // ILLEGAL
         String s2 = args[0].expandString(1);  // ILLEGAL
    }
}

Although the first parameter of expandString is a String, this actually operates on a MyProgram, and you cannot use expandString without an instance of MyProgram to operate on. Making it static means that you can (the first use of expandString in my example would become legal.)

虽然expandString的第一个参数是一个String,但它实际上是在MyProgram上运行的,并且你不能在没有MyProgram实例的情况下使用expandString。使它成为静态意味着你可以(在我的例子中首次使用expandString将变得合法。)

In general, you can't add methods to a class without modifying the source of that class. If you want to write a new method that does something with objects of a certain class C, and you can't modify class C (perhaps because it's in the Java library or someone else's library), then you'll need to put the method in some other class C2, and most of the time you will need to make the method static since it doesn't involve objects of class C2.

通常,您无法在不修改该类的源的情况下向类添加方法。如果你想编写一个新的方法,用某个类C的对象做某事,你不能修改类C(也许是因为它在Java库或其他人的库中),那么你需要把方法放在在其他一些C2类中,大多数情况下你需要使方法保持静态,因为它不涉及C2类的对象。

#2


1  

You can't call a non-static method from a static method unless you first instantiate the an object of the class.

除非首先实例化类的对象,否则不能从静态方法调用非静态方法。

e.g.

In class Whatever...

在课堂上随便......

public boolean ok() {
    return true;
}

public static void main(String[] args) {
    Whatever w = new Whatever();
    System.out.println(w.ok());
}

#3


1  

You cant call a non static method from a static context. A static method belongs to the class, non static or instance methods are copied to each instance of the class (they each have their own). If I have 10 instances of class A, and class A has a static method, which all of them share, then I try to invoke a non static method in class A from class A's static method, which instance of class A gets its method invoked? The behavior is undefined.

你不能从静态上下文中调用非静态方法。静态方法属于该类,非静态或实例方法被复制到类的每个实例(它们各自都有自己的)。如果我有10个A类实例,并且A类有一个静态方法,它们都共享,那么我尝试从类A的静态方法调用A类中的非静态方法,类A的实例调用它的方法?行为未定义。

The question really has nothing to do with arrays.

这个问题确实与数组无关。

This question is related: Can't call non static method

这个问题是相关的:不能调用非静态方法

#4


0  

I think what you're referring to is the fact that you can't extend an array class and hence can't add a method to it:

我认为你所指的是你不能扩展数组类,因此不能添加一个方法:

// No way to do this
int[] array;
array.myMethod();

This means your only option is to make a static helper method somewhere:

这意味着您唯一的选择是在某处创建一个静态帮助器方法:

// So you have to do this
int[] array;
Utils.myMethod(array);

class Utils {
    static void myMethod(int[] array) {
        ...
    }
}

An example of this is the Arrays class, which has lots of static methods for operating on arrays. Conceptually it would be clearer if these methods could be added to the array classes, but you can't do that in Java (you can in other languages like Javascript).

这方面的一个例子是Arrays类,它有许多用于在数组上操作的静态方法。从概念上讲,如果将这些方法添加到数组类中会更清楚,但是你不能在Java中这样做(你可以在其他语言中使用Javascript)。

#5


0  

That is why we use other classes like the ArrayList to make them grow dynamically in size. At the core of an arraylist you will simply find an array that is renewed in size whenever the class figures out it necessary. If I'm understanding your question right.

这就是为什么我们使用像ArrayList这样的其他类来使它们动态增长。在arraylist的核心,你只需找到一个数组,只要课程有必要,它就会更新。如果我正确理解你的问题。