# Static fields and methods

English

In some cases, calling a method with craeting an object is necessary. The main, entry point for running an application, is called without any object. You may alse want to have only a single piece of storage for a particular field, regardless of how many objects of that class are created, or even if no objects are created. A field, like total number of employees, needs only one copy, unlike employee's name, each object should have its own name.

TIP

在某些情况下,不创建对象而直接调用方法是必要的。比如,运营应用程序的主入口点就是在不创建任何对象的情况下被调用的。此外,你可能还希望为某个特定字段只分配一块存储空间,无论创建了多少个该类的对象,甚至即使没有创建任何对象。例如,像员工总数这样的字段只需要一个副本,而与员工姓名不同,每个对象都应该有自己的姓名。

English

You may need a method that isn't associated with any particular object of its class, that is, you need a method that you can call even if no objects are created. You can achieve both of these effects with the "static" keyword. When you say a field or a method is static, it mean a particular field or a method is not tied to any particular object of that class. So even if you've never created an object of that class, you can call a static method or access to at static field. To make a field or a method static, you simply place the keyword "static" before a definition.

TIP

为了实现这些需求,你可能需要一个不与其类任何特定对象像关联的方法,也就是说,你需要一个即使没有创建任何对象也可以调用的方法。你可以使用 static 关键字来实现这两个效果:静态字段和静态方法。当你说一个字段或方法是静态的,这意味着一个特定的字段或方法不绑定到该类的任何特定对象。因此,即使你从未创建过那个类的对象,你也可以调用一个静态方法或访问一个静态字段。为了声明一个字段或方法为静态的,你只需在定义前加上 static 关键字即可。