What does 'public static void' mean in Java?

后端 未结 9 1059
忘掉有多难
忘掉有多难 2020-11-29 14:29

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

9条回答
  •  旧巷少年郎
    2020-11-29 15:15

    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 since main( ) 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.

提交回复
热议问题