What does 'public static void' mean in Java?

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

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

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

    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 than NULL 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.

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

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

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