简体   繁体   English

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

[英]Why do methods on arrays have to be static (Java)?

I don't understand why methods on arrays, in Java, have to be static. 我不明白为什么Java中的数组方法必须是静态的。 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? 但是,这将是什么课程?

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. 编辑:更具体地说,我正在创建要对数组起作用的方法,但是如果我只写“ public int [] expandArr(int [] a,int v)”,然后尝试在主方法中使用此方法,我会得到错误,提示我无法对非静态方法进行静态引用。 When I write "public static int[] expandArr(int[]a, int v)" it works then. 当我编写“ 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. 谢谢。

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 . expandArr需要MyProgram的实例,因为您没有将其声明为static And that has nothing to do with arrays. 这与数组无关。 It would be the same if you wrote 如果你写的话会一样

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. 尽管expandString的第一个参数是String ,但实际上它在MyProgram上运行,并且如果没有MyProgram的实例就不能使用expandString Making it static means that you can (the first use of expandString in my example would become legal.) 将其expandString static意味着您可以(在我的示例中第一次使用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类中,大多数时候您将需要使该方法static因为它不涉及C2类的对象。

You can't call a non-static method from a static method unless you first instantiate the an object of the class. 除非首先实例化该类的对象,否则不能从静态方法调用非静态方法。

eg 例如

In class Whatever... 在课堂上...

public boolean ok() {
    return true;
}

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

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? 如果我有10个类A的实例,并且类A有一个静态方法,它们都共享,那么我尝试从类A的静态方法中调用类A中的一个非静态方法,而类A的该实例将调用其方法? The behavior is undefined. 该行为是不确定的。

The question really has nothing to do with arrays. 这个问题确实与数组无关。

This question is related: Can't call non static method 这个问题有关: 不能调用非静态方法

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. 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). 从概念上讲,是否可以将这些方法添加到数组类中会更清楚,但是您不能在Java中做到这一点(可以在其他语言(如Javascript)中做到)。

That is why we use other classes like the ArrayList to make them grow dynamically in size. 这就是为什么我们使用诸如ArrayList之类的其他类来使它们的大小动态增长的原因。 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. 如果我理解您的问题是对的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM