主类java中是静态的,构造函数中是非静态的
我只是想看看我是否能够完全理解静态的概念以及主类中静态的原因。关键字 static 指的是主类。主类中的方法之所以是静态的,是因为主类不处理对象,而是处理类本身。
然而,构造函数处理对象,因此使用非静态构造函数,因为对象具有独特的特征,将它们设为静态是没有意义的。
如果有人能看出我的陈述中是否犯了错误,或者可以引导我走向正确的方向,那将对我有很大帮助! :)
I'm just trying to see if I can fully understand the concept of static and the reason of static in a main class. The keyword static refers to the main class. the reason why methods in a main class are static is because the main class does not deal with objects but with the class itself.
However constructors deal with objects and therefore uses non static constructors because objects have unique characteristics and it would not make sense to make them static.
If anyone can see if I made a mistake in my statement or can steer me in the right direction, it would help me a great deal! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Java 中没有主类这样的东西。
不,它指的是静态类或静态类成员。
没有主类这样的东西。该声明毫无意义。
所有构造函数都是“非静态”的。不存在静态构造函数这样的东西。这种讨论毫无意义。
我认为你需要重新开始,忘记不存在的“主类”和“静态构造函数”。基本上静态方法是指无需类实例即可调用的方法。相反,构造函数创建类的实例,因此它们在逻辑上不能是静态的。
There is no such thing as a main class in Java.
No, it refers to static classes or static class members.
There is no such thing as a main class. The statement is meaningless.
All constructors are 'non-static'. There is no such thing as a static constructor. There is no point in any of this discussion.
I think you need to start again, forgetting about non-existent 'main classes' and 'static constructors'. Basically static methods refer to methods that can be invoked without having an instance of the class. Conversely, constructors create an instance of the class so they can't logically be static.
来自 Java 规范(第三版):
关于静态字段 http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.1.1
关于静态方法 http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.3.2
From Java Specification (Third edition):
About static fields http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.3.1.1
About static methods http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.3.2
不完全是...
static
意味着字段/方法等属于类,而不是该类的特定实例。该类的所有实例都可以访问静态字段,并且每个静态字段只有一个实例在实例之间共享。main 方法必须是静态的,因为它是在不先创建实例的情况下调用的。
所有静态方法都可以在不创建类实例的情况下调用 - 主方法调用的任何方法也必须是静态的,除非主方法创建类的实例 - 然后可以在该实例上调用实例方法。
Not exactly...
static
means the field/method etc belongs to the class, and not to a particular instance of the class. All instances of the class have access to static fields, and there is only one instance of each static field that is shared between instances.The main method must be static, because it is invoked without creating an instance first.
All static methods may be invoked without creating an instance of the class - any methods the main method calls must also be static, unless the main method creates an instance of the class - then instance methods may be invoked on that instance.
静态方法调用 - 当您进行静态方法调用时,您不会对类/对象内部的数据进行持久更改。
非静态方法调用 - 对于这种类型的方法调用,您必须使用构造函数方法(不能是静态的)“实例化”对象。然后可以传递一个对象。如果您的方法更改了类中字段/全局变量的值,则该值在该对象中保持不变,直到有人/其他东西更改它为止。
当您实例化一个对象时,您只能“调用”该对象内的非静态方法。同样,您不能从对象调用静态方法,您必须通过提供类名、后跟句点和方法名来调用它。
静态方法只能引用其他静态内容。非静态可以引用两者。
STATIC METHOD CALL - When you make a static method call you do not make a lasting change to the data inside of the class/object.
NON-STATIC METHOD CALL - For this type of method call you must "instantiate" the object using the Constructor method (which cannot be static). An object can then be passed around. If your method changes the value of a field/global variable in the class then that value remains the same in that object until someone/something else changes it.
When you instantiate an object you can only "invoke" (call) non-static methods inside that object. Likewise you cannot call static methods from an object you must call it by providing the class name followed by a period followed by the method name.
Static methods can only reference other static content. Non-static can reference both.