`

public static void main(String args[])

阅读更多

In core Java program, execution starts from main method when you type java main-class-name, JVM search for public static void main(String args[]) method in that class and if it doesn't find that method it throws error NoSuchMethodError:main and terminates.

First, why public? 可从任何地方访问此类成员

Propose it is private, then this method can not access outside the class, how jvm excute it?

Because main() method can excute in any java runtime evnironment, JVM need to access and execute it.

Any method or variable which is declared public in Java can be accessible from outside of that class, so declare public.

 

Second, why static? main()方法不应依赖于要创建的任何类的实例

Propose removed static, in order to excute main(), JVM has to create instance of main Class.

but constructor can be overloaded and can have arguments, ther would be no consistent way for JVM to create instance and find main method 

Thrid, why void?  不返回任何值

Main() can be overloaded, from the entry point of  class, it is not supposed to return any value.

 

 

 

 

 

分享到:
评论

相关推荐

    飞机大战java源码

    public static void main String args[] throws InterruptedException { MainFrame mainFrame; try { mainFrame new MainFrame ; mainFrame loadGame ; } catch Exception e { e printStackTrace ; } }...

    java开发飞机大战(可运行)

    public static void main String args[] throws InterruptedException { MainFrame mainFrame; try { mainFram e new MainFrame ; mainFrame loadGame ; } catch Exception e { e printStackTrace ; } } }...

    JAVA习题及实验-电子工业大学出版!

    2. `public static void main(String args[])` 是Java程序的入口点,程序从这里开始执行。 3. `int i=1, n=10, s=0;` 分别声明并初始化了三个整型变量,`i`作为循环计数器,`n`为要累加到的数,`s`用于存储累加的...

    《JavaSE知识点阶段考试题》试题和答案以及答题卡

    C、public static void main(String args) D、public static void main(String args[]) 2、下列哪个是JDK提供的编译器( )。 A、java.exe B、javac.exe C、javap.exe D、javaw.exe 3、下列那个可以作为...

    import java.io.*; public class FirstPart{ public static void main(String[] args) throws Exception{ System.out.print("The input Fahrenheit is "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int f = Integer.parseInt(br.re

    public static void main(String[] args) throws Exception{ System.out.print("The input Fahrenheit is "); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int f = Integer....

    SCJP Braindump SCJP 题库 SCJP 考题

    public static void main(String[] args) { try { return; } finally { System.out.println("Finally"); } } } ``` - **问题:** 运行结果是什么? - **答案解析:** - `finally`块在方法返回之前总是被执行...

    Java经典算法20题

    public static void main(String[] args) { for (int i = 1; i ; i++) { System.out.println(fibonacci(i)); } } public static int fibonacci(int n) { if (n == 1 || n == 2) { return 1; } else { ...

    实例分析Java中public static void main(String args[])是什么意思

    Java中的`public static void main(String[] args)`是每个可独立执行的Java程序的入口点,它的每一个部分都有特定的含义: 1. `public`:这是一个访问修饰符,表示该方法可以被任何其他类访问,无论它们是否在同一...

    java的倒序排列

    public static void main(String[] args) { String s = "abcdefg"; String t = ""; char[] charArray = s.toCharArray(); for (int i = charArray.length-1; i>=0; i--){ t=t+charArray[i]; } ...

    Java编程代码

    源代码说明Java程序设计... public static void main(String[] args) { int a=155; float b=21.0f; System.out.println("a="+a+",b="+b); //输出a,b的值 System.out.println("a/b="+(a/b)); //输出a/b的值 } }

    java的一个画图工具

    引入这个jar包 下面的代码可以调用 public static void main String args[] { Draw draw new Draw ; draw a args ; }

    Java语言程序设计资料:java题库PDF

    public static void main(String args[]){ /*---- ------*/ } System.out.println("Programming in Java is fun!"); 程序填空。 在屏幕上显示如下网格。 +---+---+ +---+---+ import java.io.*; public class Test...

    一款java2c加固混淆工具 java转c 交叉编译.dll.so

    MYJ2C将编译的Java方法转换为C语言。... public static void main(String args[]) { System.out.println("Hello, world!"); } } After public class App { public static native void main(String args[]); }

    java个人笔记总结包含45个word,绝对的原创。

    public static void main(String args[]) { A aa1=new A(); A aa2=new A(); A aa3=new A(); aa1.i=20; aa2.show(); System.out.printf("%d\n",aa3.i); } } //不再是某个对象的,是所有对象共有的。静态...

    1000以内质数的输出

    public static void main String [] args { String str ""; for int i 1; i < 1000; i++ { for a 2; a < int i 2; a++ { if i % a 0 { break; } } if a > int i 2 { str+ String ...

    jv程序设计习题.docx

    public class abc { public static void main(String args [ ]) { AB s = new AB("Hello!","I love JAVA."); } } class AB { String s1; String s2; public AB(String str1, String str2) { s1 = str1; s2 = str2; ...

    Java中的main()方法详解.doc

    在Java应用程序中,main()方法可以接收命令行参数,例如,TestMain类中main()方法的定义为:public static void main(String args[]){ System.out.println("打印main方法中的输入参数!"); for(int i=0;i<args....

    圆柱体积,用java代码

    public static void main String[] args { System out print "Enter the radius and length of a cylinder:" ; Scanner input new Scanner System in ; float radius input nextFloat ; float ...

    java 类与对象基础

    public static void main(String args[]) { BigInteger n1=new BigInteger("987654321987654321987654321"), n2=new BigInteger("123456789123456789123456789"), result=null; result=n1.add(n2);//n1和n2...

    JCancy#JAVA#01-JAVA基本概念1

    1 JAVA基本概念public static void main(String args[]){public static void main(String

Global site tag (gtag.js) - Google Analytics