java 为什么我们不需要为静态方法创建对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16229183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why we do not need to create object for static method?
提问by Rose
I know why main method is static. If it is static there is no need to instantiate that class by JVM. Without object we can call it. But why object is not needed to call static method?
我知道为什么 main 方法是静态的。如果它是静态的,则无需通过 JVM 实例化该类。没有对象我们可以调用它。但是为什么不需要对象来调用静态方法呢?
Can anyone explain please?
谁能解释一下?
回答by digitaljoel
A static method is associated with the class, not with any instance of the class.
静态方法与类相关联,而不是与类的任何实例相关联。
See http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
请参阅http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
回答by venki
Consider this example, there is a family containing a mother and three children. Mother brings three ice cream cones to each of the children, but brings only one PSP for all the three children. All children use the same PSP but they have their own ice creams.
考虑这个例子,有一个家庭,有一位母亲和三个孩子。妈妈给每个孩子带了三个冰淇淋甜筒,但只给三个孩子带了一个 PSP。所有孩子都使用相同的 PSP,但他们有自己的冰淇淋。
Here ice cream is a not-static thing (method/variable), PSP is the static thing, Mother is the class, children are objects.
这里冰淇淋是一个非静态的东西(方法/变量),PSP 是静态的东西,Mother 是类,children 是对象。
It's pretty simple. Static belongs to a class, it is common for all the objects of a class. Not-static things are object specific.
这很简单。静态属于一个类,对于一个类的所有对象都是通用的。非静态事物是特定于对象的。
回答by Ankit HTech
Object is needed for the member variables and methods but static is the application variable or function this one of the reason why object is not needed for the static.
成员变量和方法需要对象,但静态是应用程序变量或函数,这是静态不需要对象的原因之一。
回答by pickypg
Because the JVM can call the method for you (however it sees fit). Otherwise, where would the rabbit hole end? They could have done exactly what you're suggesting by creating a known interface
, itself with a main
method. For example:
因为 JVM 可以为您调用该方法(但它认为合适)。否则,兔子洞会在哪里结束?他们本可以通过interface
使用main
方法创建 known , 本身来完全按照您的建议进行操作。例如:
interface ApplicationStarter
{
void start(String []args);
}
But then there concerns related to the constructor. Numerous frameworks exist that run into similar problems, such as SPI, which requires a default (no-arg) constructor for similar reasons. Such frameworks fail when their pre-known requirements (e.g., no-arg constructor or perhaps not Serializeable
for some other frameworks), and beginners find this hard. Making the most basic part of an application "complicated" is not a good way to achieve adoption.
但随后存在与构造函数相关的问题。存在许多遇到类似问题的框架,例如 SPI,它出于类似原因需要默认(无参数)构造函数。此类框架在其预先知道的要求(例如,无参数构造函数或可能不适Serializeable
用于某些其他框架)时会失败,并且初学者发现这很难。使应用程序的最基本部分“复杂化”并不是实现采用的好方法。
For an application starting/entry point, it's far easier to depend on a known entry point (main
) that is analogous to practically every other language: no worries about the object not constructing, or overriding.
对于应用程序的起点/入口点,依赖一个已知的入口点 ( main
)要容易得多,这与几乎所有其他语言类似:不用担心对象没有构造或覆盖。
回答by Lingasamy Sakthivel
The main()
method is static because they can then be invoked by the runtime engine without having to instantiate an instance of the parent class.
该main()
方法是静态的,因为它们可以由运行时引擎调用,而无需实例化父类的实例。
Static
methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class
Static
在声明中带有 static 修饰符的方法应该使用类名调用,而不需要创建类的实例
回答by queueoverflow
Yes you are correct that you don't need an instance object to call the static method of a class, because static methods belongs to a class, and not to the instance of that class. Also, you cannot use instance variables inside the static method because instance variables belong to the instance.
是的,您不需要实例对象来调用类的静态方法是正确的,因为静态方法属于类,而不属于该类的实例。此外,您不能在静态方法中使用实例变量,因为实例变量属于实例。
回答by user2512425
When we execute a java file, a java compiler loads and executes the static member automatically.
当我们执行一个 java 文件时,java 编译器会自动加载并执行静态成员。
I'm new in java so please forgive me if my answer is wrong.
我是 Java 新手,所以如果我的回答有误,请原谅我。