------- android培训、java培训、期待与您交流! ----------
关于Static静态方法的一些注意事项
案例:
class Demo
{
public static void main(String args[])
{
/**
创建一个对象对象
Xing a=new Xing();
a.ju(4,5);
*/
//出现错误,没有建立对象,不能将Xing中的非静态方法进行Static静态引用;
//静态方法始终都存在,非静态方法只有在建立对象时候才存在。
Xing.ju(4,5);
}
}
class Xing
{
//以下为非静态方法,不能在主函数中直接调用。
public void ju(int a,int b)
{
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
System.out.print("*"+" ");
}
System.out.println("\n");
}
}
}
------------------------------------------------------------
改:
class Demo
{
public static void main(String args[])
{
Xing.ju(4,5);
}
}
class Xing
{
public static void ju(int a,int b)
{
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
System.out.print("*"+" ");
}
System.out.println("\n");
}
}
}