Java类中的static方法和static变

来源:互联网 发布:js 解码ccitt.t6 编辑:程序博客网 时间:2024/05/17 04:43
定义static变量是称为静态变量
理解静态变量必须先理解静态方法因为静态方法可以通过类名直接调用例public class A{   public static void print(){      System.out.println("Hello World");}}如果A类里的print方法没有带static则调用的话必须是先创建A类的实例化对象再使用A a=new A();a.print();我列举的A类里带了static 所以调用的时候直接通过类名调这个方法而无须实例化对象A.print();说完了static方法我们再来看看static变量本身来说static变量是没有特殊意义的。声明它只是为了供static方法使用.因为static方法体里所有的变量都必须是static引用刚才的例子public class A{      private static String str="Hello World";   public static void print(){      System.out.println(str);    }}因为print方法是static的,所以在它里面使用的变量必须是static
原创粉丝点击