Java基础之静态变量

如下面代码;

public class Test1 
{
	private int n1 = 0;
	private static int n2 = 0;
	
	public void setNum()
	{
		for(int i=0; i<100; i++)
		{
			n1++;
			n2++;
		}
		
		System.out.println("n1=" + n1);
		System.out.println("n2=" + n2);
	}
}

 

 

下面是测试代码:

public class Test2 
{
	public static void main(String[] args)
	{
		new Test1().setNum();
		
		new Test1().setNum();
		
		new Test1().setNum();
	}
}

 

输出结果为:

n1=100
n2=100
n1=100
n2=200
n1=100
n2=300

 

 

你可能感兴趣的:(java)