“public static void”是什么意思?在Java中是什么意思?
Java中public static void
是什么意思?
我正在学习过程中。在本书的所有示例中,我正在使用的 public static void
位于正在使用或创建的任何方法之前。这意味着什么?
What does public static void
mean in Java?
I'm in the process of learning. In all the examples in the book I'm working from public static void
comes before any method that is being used or created. What does this mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是三个完全不同的东西:
public
意味着该方法是可见的并且可以从其他类型的其他对象调用。其他替代方案包括private
、protected
、package
和package-private
。有关详细信息,请参阅此处。static
意味着该方法与类相关联,而不是与该类的特定实例(对象)相关联。这意味着您可以调用静态方法而无需创建该类的对象。void
表示该方法没有返回值。如果该方法返回int
,您将编写int
而不是void
。所有这三种方法的组合最常见于大多数教程都会包含的
main
方法。It's three completely different things:
public
means that the method is visible and can be called from other objects of other types. Other alternatives areprivate
,protected
,package
andpackage-private
. See here for more details.static
means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.void
means that the method has no return value. If the method returned anint
you would writeint
instead ofvoid
.The combination of all three of these is most commonly seen on the
main
method which most tutorials will include.这三个词具有正交的含义。
public
意味着该方法对于其他包中的类是可见的。static
表示该方法未附加到特定实例,并且它没有“this
”。它或多或少是一个功能。void
是返回类型。它的意思是“这个方法什么都不返回”。The three words have orthogonal meanings.
public
means that the method will be visible from classes in other packages.static
means that the method is not attached to a specific instance, and it has no "this
". It is more or less a function.void
is the return type. It means "this method returns nothing".public 关键字是一个访问说明符,它允许程序员控制类成员的可见性。当类成员前面带有 public 时,则该成员可以被声明它的类外部的代码访问。 (public 的反义词是 private,它可以防止成员被其类外部定义的代码使用。)
在这种情况下,
main( )
必须声明为 public,因为它必须由程序启动时在其类之外的代码。关键字static允许调用
main()
而不必实例化该类的特定实例。这是必要的,因为main( )
是在创建任何对象之前由 Java 解释器调用的。关键字void只是告诉编译器
main()
不返回值。正如您将看到的,方法也可能返回值。The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. (The opposite of public is private, which prevents a member from being used by code defined outside of its class.)
In this case,
main( )
must be declared as public, since it must be called by code outside of its class when the program is started.The keyword static allows
main( )
to be called without having to instantiate a particular instance of the class. This is necessary sincemain( )
is called by the Java interpreter before any objects are made.The keyword void simply tells the compiler that
main( )
does not return a value. As you will see, methods may also return values.这意味着:
public
- 可以从任何地方调用它static
- 它没有任何对象状态,因此您可以在不实例化对象的情况下调用它void
- 它不返回任何内容您可能会认为缺少返回意味着它没有做太多事情,但例如它可能会在数据库中保存内容。
It means that:
public
- it can be called from anywherestatic
- it doesn't have any object state, so you can call it without instantiating an objectvoid
- it doesn't return anythingYou'd think that the lack of a return means it isn't doing much, but it might be saving things in the database, for example.
这意味着三件事。
第一个
public
意味着任何其他对象都可以访问它。static
意味着在调用函数之前不必先实例化它所在的类。void
表示该函数没有返回值。由于您刚刚学习,所以在您学习类之前不要太担心前两个,而在您开始编写函数(除了 main 之外)之前,第三个并不重要。
我在学习编程时得到的最好的建议,也是我传递给你的,是不要担心你不能立即理解的小细节。对基本原理有一个广泛的概述,然后再回头考虑细节。原因是您必须在您的第一个程序中使用一些东西(例如 public static void ),如果不先教您一堆其他东西,这些东西就无法真正解释清楚。因此,目前,只需接受这就是它的完成方式,然后继续前进。您很快就会理解它们。
It means three things.
First
public
means that any other object can access it.static
means that the class in which it resides doesn't have to be instantiated first before the function can be called.void
means that the function does not return a value.Since you are just learning, don't worry about the first two too much until you learn about classes, and the third won't matter much until you start writing functions (other than main that is).
Best piece of advice I got when learning to program, and which I pass along to you, is don't worry about the little details you don't understand right away. Get a broad overview of the fundamentals, then go back and worry about the details. The reason is that you have to use some things (like
public static void
) in your first programs which can't really be explained well without teaching you about a bunch of other stuff first. So, for the moment, just accept that that's the way it's done, and move on. You will understand them shortly.考虑典型的顶级课程。顶层只能使用 public 和 nomodifier 访问修饰符,因此您要么看到 public,要么根本看不到任何访问修饰符。
使用“static”是因为您可能不需要在顶层创建实际的对象
(但有时您会想要这样做,因此您可能并不总是看到/使用静态。还有其他原因导致您不包含静态,但这是顶层的典型原因。)
void
是使用它是因为通常您不会从顶层(类)返回值。 (有时你会想要返回一个 NULL 以外的值,因此 void 可能并不总是被使用,特别是当你在顶层声明、初始化一个对象并分配一些值时到)。免责声明:
我自己也是个新手,所以如果这个答案有任何错误,请不要挂我。白天我是一名技术招聘人员而不是开发人员;编码是我的爱好。另外,我总是乐于接受建设性的批评,并且热爱学习,所以请随时指出任何错误。
Considering the typical top-level class. Only public and no modifier access modifiers may be used at the top level so you'll either see public or you won't see any access modifier at all.
`static`` is used because you may not have a need to create an actual object at the top level
(but sometimes you will want to so you may not always see/use static. There are other reasons why you wouldn't include static too but this is the typical one at the top level.)
void
is used because usually you're not going to be returning a value from the top level (class). (sometimes you'll want to return a value other thanNULL
so void may not always be used either especially in the case when you have declared, initialized an object at the top level that you are assigning some value to).Disclaimer:
I'm a newbie myself so if this answer is wrong in any way please don't hang me. By day I'm a tech recruiter not a developer; coding is my hobby. Also, I'm always open to constructive criticism and love to learn so please feel free to point out any errors.
Public
- 意味着该类(程序)可供任何其他类使用。静态
- 创建一个类。还可以应用于变量和方法,使它们成为类方法/变量,而不仅仅是类的特定实例的本地变量。Void
- 这意味着类完成处理时不会返回任何产品。与向主类提供返回值的辅助类相比,它们的操作类似于函数;这些在声明中不存在无效。Public
- means that the class (program) is available for use by any other class.Static
- creates a class. Can also be applied to variables and methods,making them class methods/variables instead of just local to a particular instance of the class.Void
- this means that no product is returned when the class completes processing. Compare this with helper classes that provide a return value to the main class,these operate like functions; these do not have void in the declaration.public
表示您可以从类/对象中或包或类外部的任何位置访问该类static
表示语句块仅使用 1 次的常量void
表示无返回类型public
means you can access the class from anywhere in the class/object or outside of the package or classstatic
means constant in which block of statement used only 1 timevoid
means no return typestatic
意味着该方法与类相关联,而不是与该类的特定实例(对象)相关联。这意味着您可以调用静态方法而无需创建该类的对象。由于使用了
static
关键字main()
是您要调用的第一个方法。static
不需要任何对象来实例化...因此,
main( )
在创建任何对象之前由 Java 解释器调用。static
means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.Because of use of a
static
keywordmain()
is your first method to be invoked..static
doesn't need to any object to instance...so,
main( )
is called by the Java interpreter before any objects are made.