class StaticDemo
{
static int count ;
static
{
count ++;
System.out.println(
count);
}
}
class ObjectDemo
{
public static void main(String[] args)
{
new StaticDemo();运行,输出
1
}
}
下列情况需要被定义为静态方法
函数没有访问到对象的特有数据,即代码上没有用到非静态成员变量,一般定义为静态方法如果不需要访问非静态变量也可以定义为非静态方法,但是非静态方法只能被对象所调用,为了调用一个没有访问到特有数据的方法而创建一个对象是没有意义的
转载请注明原文地址: https://www.6miu.com/read-250202.html