java中静态成员与非静态成员

来源:互联网 发布:mac系统怎么重装win10 编辑:程序博客网 时间:2024/05/29 18:02

java编程中,对静态变量的修改会影响到所有对象,静态成员的一个例子:

package Test;public class StaticTester {    public static void main(String[] args){        StaticTest.y+=1;        StaticTest t1 = new StaticTest();        StaticTest t2 = new StaticTest();        t1.x+=1;        t1.y+=1;        t2.x+=2;        t2.y+=2;        System.out.println("T1:x= " + t1.x +",y = " + t1.y);        System.out.println("T1:x= " + t2.x +",y = " + t2.y);    }}class StaticTest{    public int x=1;    public static int y = 1;}

输出如图:
这里写图片描述

原创粉丝点击