What does 'public static void' mean in Java?

后端 未结 9 1020
忘掉有多难
忘掉有多难 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:11

    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 are private, protected, package and package-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 an int you would write int instead of void.

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

    0 讨论(0)
  • 2020-11-29 15:12
    • public means you can access the class from anywhere in the class/object or outside of the package or class
    • static means constant in which block of statement used only 1 time
    • void means no return type
    0 讨论(0)
  • 2020-11-29 15:14

    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.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-29 15:19

    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

    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.

    0 讨论(0)
  • 2020-11-29 15:20

    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.

    0 讨论(0)
提交回复
热议问题