public class StaticDemo08{
public static void main(String args[]){
fun();
}
public static void fun(){ //可以由主方法直接调用
System.out.println("hello world!");
}
};
public void fun(){ //不能由主方法直接调用
System.out.println("hello world!");
}
如不加static定义方法,则main方法不能调用。
public class StaticDemo08{
public static void main(String args[]){
if(args.length!=3){ // 输入的参数如果不足3个,则出错
System.out.println("输入的参数不足三个,程序退出~") ;
System.exit(1) ; // 直接退出此程序
}
for(int i=0;i<args.length;i++){ // 循环输出输入的参数
System.out.println("第"+(i+1)+"个参数:" + args[i]) ;
}
}
};
java StaticDemo08 "hello world" two three

本文通过两个示例介绍了Java中静态方法的使用及如何处理命令行参数。首先展示了如何定义静态方法并从主方法调用它。其次,演示了如何读取并验证传入程序的命令行参数数量,并逐个打印这些参数。

被折叠的 条评论
为什么被折叠?



