这是为所有初学者解释静态关键字的一点差异。
当您更多地使用类和对象时,您会清楚地了解它。
|*| 静态:静态项目可以用类名调用
如果你在代码中观察,一些函数直接用类名调用,如
NamCls.NamFnc();
System.out.println();
这是因为 NamFnc 和 println 将在它们之前使用关键字 static 声明。
|*| 非静态:非静态项目可以用类变量调用
如果它不是静态的,你需要一个类的变量,
在类变量后面加上点,
然后调用函数。
NamCls NamObjVar = new NamCls();
NamObjVar.NamFnc();
下面的代码巧妙地解释了你
|*| 类中的静态和非静态函数:
public class NamCls
{
public static void main(String[] args)
{
PlsPrnFnc("Tst Txt");
NamCls NamObjVar = new NamCls();
NamObjVar.PrnFnc("Tst Txt");
}
static void PlsPrnFnc(String SrgPsgVal)
{
System.out.println(SrgPsgVal);
}
void PrnFnc(String SrgPsgVal)
{
System.out.println(SrgPsgVal);
}
}
|*| 类中的静态和非静态类:
public class NamCls
{
public static void main(String[] args)
{
NamTicCls NamTicVaj = new NamTicCls();
NamTicVaj.PrnFnc("Tst Txt");
NamCls NamObjVar = new NamCls();
NamNicCls NamNicVar = NamObjVar.new NamNicCls();
NamNicVar.PrnFnc("Tst Txt");
}
static class NamTicCls
{
void PrnFnc(String SrgPsgVal)
{
System.out.println(SrgPsgVal);
}
}
class NamNicCls
{
void PrnFnc(String SrgPsgVal)
{
System.out.println(SrgPsgVal);
}
}
}