Java基础

it2022-05-05  148

变量的分类(3类)

局部变量: 位置:方法或语句块内部定义的变量。 生命周期:从声明位置开始到方法或语句块执行完毕为止。 成员变量:(也叫做实例变量) 位置:方法外部,类的内部定义的变量,从属于对象。 生命周期:伴随对象始终。 静态变量:(也叫做类变量) 位置:从属于类。 生命周期:伴随类从始到终。

局部变量

// main函数中的局部变量 public class text { public static void main(String[] args) { int sum=2;//只在main函数中有效果 //语句块 { int body; } }

成员变量

// main函数中的局部变量 public class text { int txt;//该变量是成员变量 public static void main(String[] args) { int sum=2;//只在main函数中有效果 //语句块 { int body; } }

静态变量

public class Chat { private static CopyOnWriteArrayList<Channel> all =new CopyOnWriteArrayList<Channel>(); public static void main(String[] args) throws IOException { System.out.println("-----Server-----"); // 1、指定端口 使用ServerSocket创建服务器 ServerSocket server =new ServerSocket(8888); // 2、阻塞式等待连接 accept while(true) { Socket client =server.accept(); System.out.println("一个客户端建立了连接"); Channel c =new Channel(client); all.add(c); //管理所有的成员 new Thread(c).start(); } }

最新回复(0)