JAVA static 关键字

来源:互联网 发布:慧算账软件好用吗 编辑:程序博客网 时间:2024/05/16 03:30

如果要想将一个属性设置成公共属性,则就需要使用static 关键字进行声明。



package org.staticdemo;class Personb{private String name;static String city = "A城";public Personb(String name){this.name = name;}public void print(){System.out.println("name:"+this.name+","+"city:"+city);}}public class StaticDemo01 {public static void main(String args[]){Personb per1 = new Personb("张三");Personb per2 = new Personb("李四");per1.print();per2.print();// per1.city = "B城";Personb.city = "B城";per1.print();per2.print();}}


静态方法:


package org.staticdemo;class Personb{private String name;private static String city = "A城";public static void setCity(String c){city = c; }public Personb(String name){this.name = name;}public void print(){System.out.println("name:"+this.name+","+"city:"+city);}}public class StaticDemo01 {public static void main(String args[]){Personb per1 = new Personb("张三");Personb per2 = new Personb("李四");per1.print();per2.print();// per1.city = "B城";Personb.setCity ("B城");per1.print();per2.print();}}

在使用静态方法时需要注意以下几点:

static 的方法只能调用static 的属性或方法,不能调用非static 的属性或方法


public static void main(String args[])

public :表示最大的权限,所有人都可以访问

static:因为执行的时候执行的就是类名称,所以表示可以由类名称直接调用

void:因为主方法是一切的起点,所以表示没有返回值

main:系统内建的方法名称

String args[]:表示字符串数组,用于接收参数


栈内存:对象名称,实际上是对象的引用地址

堆内存:属性

全局代码区:保存所有的操作方法

全局数据区:保存所有的static 属性


统计实例化次数:


package org.staticdemo;class PersonC {private String name;private static int count;public PersonC(String name){count++;this.name = name;System.out.println("实例化次数"+count);}}public class StaticDemo02 {public static void main(String args[]){new PersonC("张三");new PersonC("张三");new PersonC("张三");new PersonC("张三");new PersonC("张三");}}

属性动态赋值:


package org.staticdemo;class PersonC {private String name;private static int count;public PersonC(){count++;this.name = "NOName"+count;}public String getName(){return this.name;}}public class StaticDemo02 {public static void main(String args[]){PersonC per1 = new PersonC();System.out.println(per1.getName());PersonC per2 = new PersonC();System.out.println(per2.getName());PersonC per3 = new PersonC();System.out.println(per3.getName());PersonC per4 = new PersonC();System.out.println(per4.getName());}}


在JAVA 中使用{} 括起来的代码称代码块

普通代码块:直接在一个方法中出现{} 就称为普通代码块

构造块:在类中定义的代码块 (重复调用多次,构造块会优先于构地造方法)

静态模块: 使用static 关键字声明的代码块 主要目的是用来为静态属性初始化 (静态块优先于主方法执行,静态块优先于构造块执行,而且只执行一次)

同步代码块


package org.staticdemo;public class StaticDemo02 {public static void main(String args[]){{ //普通代码块int x = 10;System.out.println(x);}}}


package org.staticdemo;class Person{{ // 构造块System.out.println("构造块");}public Person(){// 构造块System.out.println("构造方法");}}public class StaticDemo02 {public static void main(String args[]){{int x = 10;System.out.println(x);}}}

package org.staticdemo;class Person{{ // 构造块System.out.println("构造块");}public Person(){// 构造块System.out.println("构造方法");}static{System.out.println("类中的静态块");}}public class StaticDemo02 {static{System.out.println("主类中的静态块");}public static void main(String args[]){{int x = 10;System.out.println(x);}}}


原创粉丝点击