Java中的“public static void”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2390063/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:52:33  来源:igfitidea点击:

What does 'public static void' mean in Java?

javamethods

提问by David

What does public static voidmean in Java?

public static void在 Java中是什么意思?

I'm in the process of learning. In all the examples in the book I'm working from public static voidcomes before any method that is being used or created. What does this mean?

我正在学习中。在我public static void正在使用的书中的所有示例中,都出现在任何正在使用或创建的方法之前。这是什么意思?

采纳答案by Mark Byers

It's three completely different things:

这是三个完全不同的东西:

publicmeans that the method is visible and can be called from other objects of other types. Other alternatives are private, protected, packageand package-private. See herefor more details.

public意味着该方法是可见的,可以从其他类型的其他对象调用。其他的选择是privateprotectedpackagepackage-private。请参阅此处了解更多详情。

staticmeans 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.

static意味着该方法与类相关联,而不是该类的特定实例(对象)。这意味着您可以在不创建类的对象的情况下调用静态方法。

voidmeans that the method has no return value. If the method returned an intyou would write intinstead of void.

void意味着该方法没有返回值。如果该方法返回一个,int您将编写int而不是void.

The combination of all three of these is most commonly seen on the mainmethod which most tutorials will include.

所有这三者的组合在main大多数教程将包含的方法中最常见。

回答by Thomas Pornin

The three words have orthogonal meanings.

这三个词具有正交的含义。

publicmeans that the method will be visible from classes in other packages.

public意味着该方法将从其他包中的类中可见。

staticmeans that the method is not attached to a specific instance, and it has no "this". It is more or less a function.

static表示该方法不附加到特定实例,并且没有“ this”。它或多或少是一个函数。

voidis the return type. It means "this method returns nothing".

void是返回类型。这意味着“此方法不返回任何内容”。

回答by Paul Tomblin

It means that:

这意味着:

  • public- it can be called from anywhere
  • static- it doesn't have any object state, so you can call it without instantiating an object
  • void- it doesn't return anything
  • public- 它可以从任何地方调用
  • static- 它没有任何对象状态,因此您可以在不实例化对象的情况下调用它
  • void- 它不返回任何东西

You'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.

例如,您可能认为没有返回意味着它没有做太多事情,但它可能会在数据库中保存一些东西。

回答by Aaron

It means three things.

这意味着三件事。

First publicmeans that any other object can access it.

首先public意味着任何其他对象都可以访问它。

staticmeans that the class in which it resides doesn't have to be instantiated first before the function can be called.

static意味着在调用函数之前不必先实例化它所在的类。

voidmeans that the function does not return a value.

void意味着该函数不返回值。

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).

由于您只是在学习,所以在您了解类之前不要太担心前两个,而在您开始编写函数(除了 main 之外)之前,第三个无关紧要。

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 static void在你的第一个程序中使用一些东西(比如),如果不先教你一些其他东西,就不能很好地解释这些东西。所以,就目前而言,接受它就是这样做的,然后继续前进。你很快就会理解它们。

回答by user647200

Public- means that the class (program) is available for use by any other class.

Public- 表示该类(程序)可供任何其他类使用。

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.

Static- 创建一个类。也可以应用于变量和方法,使它们成为类方法/变量,而不仅仅是类的特定实例的局部。

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.

Void- 这意味着类完成处理后不会返回任何产品。将此与向主类提供返回值的辅助类进行比较,它们的操作类似于函数;这些在声明中没有无效。

回答by Saleem Bhayo

  • publicmeans you can access the class from anywhere in the class/object or outside of the package or class
  • staticmeans constant in which block of statement used only 1 time
  • voidmeans no return type
  • public意味着您可以从类/对象中或包或类之外的任何位置访问该类
  • static表示常量,其中语句块仅使用 1 次
  • void意味着没有返回类型

回答by David Leak New York NY

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.

考虑典型的顶级班级。在顶层只能使用 public 而没有修饰符访问修饰符,因此您要么看到 public 要么根本看不到任何访问修饰符。

`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.)

使用 `static` 是因为你可能不需要在顶层创建一个实际的对象(但有时你会想要这样你可能并不总是看到/使用静态。还有其他原因为什么你不包括静态也是,但这是顶级的典型。)

voidis 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 than NULLso 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).

void使用是因为通常您不会从顶级(类)返回值。(有时您会想要返回一个NULL除此之外的值,因此 void 可能并不总是被使用,尤其是在您已经声明、初始化要为其分配某些值的顶层对象的情况下)。

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.

免责声明:我自己是一个新手,所以如果这个答案有任何错误,请不要挂我。白天,我是一名技术招聘人员,而不是一名开发人员;编码是我的爱好。另外,我总是乐于接受建设性的批评并喜欢学习,所以请随时指出任何错误。

回答by utsav

The publickeyword 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.)

公共关键字是一个访问说明符,它允许程序员控制类成员的可见性。当类成员前面有 public 时,该成员可以被声明它的类之外的代码访问。(公共的反面是私有的,它可以防止成员被在其类之外定义的代码使用。)

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.

在这种情况下,main( )必须声明为 public,因为它必须在程序启动时由其类之外的代码调用。

The keyword staticallows main( )to be called without having to instantiate a particular instance of the class. This is necessary since main( )is called by the Java interpreter before any objects are made.

关键字static允许main( )在不必实例化类的特定实例的情况下调用。这是必要的,因为main( )在创建任何对象之前由 Java 解释器调用。

The keyword voidsimply tells the compiler that main( )does not return a value. As you will see, methods may also return values.

关键字void只是告诉编译器main( )不返回值。正如您将看到的,方法也可能返回值。

回答by mahi

staticmeans 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 statickeyword main()is your first method to be invoked.. staticdoesn't need to any object to instance... so,main( )is called by the Java interpreter before any objects are made.

static意味着该方法与类相关联,而不是该类的特定实例(对象)。这意味着您可以在不创建类的对象的情况下调用静态方法。因为使用static关键字main()是您要调用的第一个方法...... static不需要任何对象来实例......所以,main( )在创建任何对象之前由Java解释器调用。