package week2;
class w{
int ca(int height,int width)
{
return height*width;
}
}
public class Week2 {
public static void main(String[] args) {
w a=new w(); //创建对象(实例化)
a.ca(2, 3);
}
}
static的main方法 要通过某个对象来调用方法
public class Week2 {
static int ca(int height,int width)
{
return height*width;
}
public static void main(String[] args) {
{
ca(2,3)
}
}
静态方法里面不能调用非静态的方法
这种在一个类里的static方法的调用就可以直接调用
或者要是也创建个对象(实例化),java编译器会有个警告
The static method ca(int, int) from the type Week2 should be accessed in a static way-------应该以静态方式访问来自类型周长的静态方法CA(int,int)
但是不影响编译。