简体   繁体   中英

What does `public static <T> void main(String[] args)` stand for?

What does public static <X> void main(String[] args) stand for? I tried to understand but didn't get. I know about public static void main(String[] arg) . Thanks in advance.

Let's look at each bit in turn:

  • public - it's a public method, accessible to anything which has access to the class in which this is declared
  • <X> - this is (somewhat bizarrely) a generic method with an unbound type variable X
  • static - the method is related to the type in which it's declared, not any specific instance of the type
  • void - the method doesn't return a value
  • main - the name of the method
  • String[] args - a single parameter, of type String[] and called args

main is the entry point used by the JVM. When you run:

java foo.bar.Baz

it will try to find a main method in class foo.bar.Baz . I've never seen a generic main method before, admittedly. For more about generics in Java, read the Java Generics FAQ .

  1. <X> is known as Type Parameter .

  2. This is applicable to the methods, classes, variables, etc.. But its most important use is to make the Collections more type safe.

  3. <X> will mark a certain Type within the main() method.

  4. The whole sentence dissection is as follows:

    • public - is the access-modifier, means that this method is accessible from anywhere.
    • <X> - Type Parameter, as mentioned above
    • void - This method will not return anything
    • main - Method's name, main() method is the entry point of any pgm in java.
    • String[] : Array Of String.
    • args : Array reference variabl of type String.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM