java 的静态调用:
public class hhj{
public static void main(String[] args ) {
int b = getValue(2);
System.out.println(b);
}
public static int getValue(int i) {
int result = 0;
switch (i) {
case 1:
result = result + i;
case 2:
result = result + i * 2;
case 3:
result = result + i * 3;
}
return result;
}
}
注意这里面的b是直接赋值的类似于C语言中函数的使用;
但是注意注意注意 调用的方法必须是静态的;什么是静态的?
public static int getValue(int i){
}
就是要在public后加static
如果不加,类似C语言使用会报错:
在方法int getValue(int i)的前面加上static 就可以解决了;
问:
类中自定义非static方法,代码放在该方法内;main方法中调用上述非static方法 怎么办?
public class hhj{
public static void main(String[] args ) {
int b = new hhj().getValue(2);//类的引用,不能引入 不加 hhj(). 而直接引用 getValue(),也不能不创建new;
System.out.println(b);
}
public int getValue(int i) {
int result = 0;
switch (i) {
case 1:
result = result + i;
case 2:
result = result + i * 2;
case 3:
result = result + i * 3;
}
return result;
}
}
方法的定义可以在主函数的上方,也可以在主函数的下方;
一个类中后面如果多加了一个 } ,报错:
java中的八进制是前面加0;不能用int a= 078;
会报错;