[英]Understanding static methods in Java
Can someone help me understand what does the following statement mean?有人可以帮助我理解以下语句的含义吗?
"Like any method, a static method can create or use named objects of its type , so a static method is often used as a “shepherd” for a flock of instances of its own type . " “与任何方法一样,静态方法可以创建或使用其类型的命名对象,因此静态方法通常用作其自身类型实例群的“牧羊人” 。”
Source: http://www.codeguru.com/java/tij/tij0037.shtml#Heading79来源: http : //www.codeguru.com/java/tij/tij0037.shtml#Heading79
Here's an example: say you have a class Person
that looks like this:下面是一个例子:假设你有一个类似这样的
Person
类:
public class Person {
static ArrayList<Person> people = new ArrayList<>();
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
people.add(this);
}
public void display() {
System.out.println(name + ", age " + age);
}
public static void displayAll() {
for (int i=0; i<people.size(); i++) {
people.get(i).display();
}
}
}
In this example, people
belongs to the Person
class itself because it is static, whereas name
and age
are non-static and belong to each instance
of Person
.在这个例子中,
people
属于Person
类本身,因为它是静态的,而name
和age
是非静态的,属于Person
每个instance
。 Similarly, because displayAll()
is static, it can only be called by Person
, whereas the non-static display()
can only be called by individual instances of Person
.同样,因为
displayAll()
是静态的,它只能被Person
调用,而非静态的display()
只能被Person
的单个实例调用。
To illustrate this, say you have this in your main class:为了说明这一点,假设您在主类中有这个:
Person john = new Person("John", 25);
Person amy = new Person("Amy", 27);
System.out.println(john.name + " is " + john.age);
System.out.println(amy.name + " is " + amy.age);
This would create two instances of Person
, john
and amy
, and would give the following output:这将创建
Person
两个实例john
和amy
,并给出以下输出:
John is 25
Amy is 27
The following code would also work (assume for all examples from here on that john
and amy
have already been created as in the previous example):下面的代码也可以工作(假设这里的所有示例
john
和amy
都已经像前面的示例一样创建了):
john.display();
amy.display();
This would give this output:这将给出以下输出:
John, age 25
Amy, age 27
Now, because john
and amy
are specific instances of Person
, they cannot reference static variables or call static methods, so both of these next lines of code would be uncompilable:现在,因为
john
和amy
是Person
特定实例,它们不能引用静态变量或调用静态方法,因此接下来的两行代码都将无法编译:
System.out.println(john.people.size());
amy.displayAll();
However, the following would work:但是,以下方法可行:
System.out.println(Person.people.size());
Person.displayAll();
This would give this output:这将给出以下输出:
2
John, age 25
Amy, age 27
However, the following would NOT work:但是,以下方法不起作用:
Person.display();
System.out.println(Person.name);
System.out.println(Person.age);
Person.display()
does not work because display()
is not a static method. Person.display()
不起作用,因为display()
不是静态方法。 The next two lines don't work because the variables name
and age
belong to specific instances of Person
and do not apply to the Person
class in general.接下来的两行不起作用,因为变量
name
和age
属于Person
特定实例,通常不适用于Person
类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.