嗨,有人可以向我解释为什么在Java中的主函数调用非静态方法之前必须创建实例吗?这背后的原因是什么?
解决方法:
因为它们是实例成员,所以要访问它们,您需要实例.
When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables. In the case of the Bicycle class, the instance variables are cadence, gear, and speed. Each Bicycle object has its own values for these variables, stored in different memory locations.
所以现在您关于静态的第二个问题
Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
标签:instance,static,main,java,methods
来源: https://codeday.me/bug/20191122/2062605.html