美文网首页Hello Java
static关键字修饰类

static关键字修饰类

作者: Aldeo | 来源:发表于2018-09-15 00:03 被阅读6次

    java里面static一般用来修饰成员变量或函数。但有一种特殊用法是用static修饰内部类,普通类是不允许声明为静态的,只有内部类才可以。

    被static修饰的内部类可以直接作为一个普通类来使用,而不需实例一个外部类(见如下代码):

    需要注意的是当一个内部类没有使用static修饰的时候,是不能直接使用内部类创建对象,须要先使用外部类对象点new内部类对象及(外部类对象.new 内部类())

    [ 复制代码

    ](javascript:void(0); "复制代码")

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">public class OuterClass { 2 public static class InnerClass{ 3 InnerClass(){ 4 System.out.println("============= 我是一个内部类'InnerClass' ============="); 5 } 6 } 7 } 8
    9
    10 public class TestStaticClass { 11 public static void main(String[] args) { 12 // 不需要new一个InnerClass
    13 new OuterClass.InnerClass(); 14 } 15 }</pre>

    [ 复制代码

    ](javascript:void(0); "复制代码")

    如果没有用static修饰InterClass,则只能按如下方式调用:需要先new 一个外部类实例

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">OuterClass oc = new OuterClass(); 在使用外部类实例点内部类实例</pre>

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; color: rgb(51, 51, 51); font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial;">oc.new InnerClass();</pre>

    [ 复制代码

    ](javascript:void(0); "复制代码")

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">package inner_class; 2
    3 public class OuterClass { 4 public class InnerClass{ 5 InnerClass(){ 6 System.out.println("============= 我是一个内部类'InnerClass' ============="); 7 } 8 } 9 } 10
    11 public class TestStaticClass { 12 public static void main(String[] args) { 13 // OutClass需要先生成一个实例,然后再new一个InnerClass();
    14 OuterClass oc = new OuterClass(); 15 oc.new InnerClass(); 16 } 17 }</pre>

    [ 复制代码

    ](javascript:void(0); "复制代码")

    注意:如果内部类没事有使用static 修饰,是不能创建实例的回报错 入下图代码所示

    [ 复制代码

    ](javascript:void(0); "复制代码")

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">public class SyncDubbo2 { static class Main { public int i = 10; public synchronized void operationSup(){ try {
    i--;
    System.out.println("Main print i = " + i);
    Thread.sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    } class Sub extends Main { //没有使用static修饰的内部类不能直接创建对象 public synchronized void operationSub(){ try { while(i > 0) {
    i--;
    System.out.println("Sub print i = " + i);
    Thread.sleep(100); this.operationSup();
    }
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    } public static void main(String[] args) {

        Thread t1 = new Thread(new Runnable() {
            @Override public void run() {  
                Sub sub = new Sub(); //此处内部类没用static修饰会报错误The value of the local variable sub is not used</pre>
    

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;"> //没有使用static修饰的内部类不能直接创建对象</pre>

    <pre style="margin: 0px; padding: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">} }); t1.start(); } }</pre>

    [ 复制代码

    ](javascript:void(0); "复制代码")

    相关文章

      网友评论

        本文标题:static关键字修饰类

        本文链接:https://www.haomeiwen.com/subject/xqmgbftx.html