Java static

作者: JaedenKil | 来源:发表于2018-01-05 16:50 被阅读1次

Static variable

public class TryNonStatic {
    int x = 0;
    TryNonStatic() {
        x ++;
        System.out.println(x);
    }

    public static void main(String[] args) {
        TryNonStatic t1 = new TryNonStatic();
        TryNonStatic t2 = new TryNonStatic();
        t2.x += 1;
        TryNonStatic t3 = new TryNonStatic();
    }
}
1
1
1

In this above example, we have created an instance variable named count which is incremented in the constructor. Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable, if it is incremented, it won't reflect to other objects. So each objects will have the value 1 in the count variable.

public class TryStatic {
    static int x = 0;
    TryStatic() {
        x ++;
        System.out.println(x);
    }

    public static void main(String[] args) {
        TryStatic t1 = new TryStatic();
        TryStatic t2 = new TryStatic();
        TryStatic t3 = new TryStatic();
    }
}

1
2
4

Static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.

Static method

If you apply static keyword with any method, it is known as static method.

  • A static method belongs to the class rather than object of a class.
  • A static method can be invoked without the need for creating an instance of a class.
  • static method can access static data member and can change the value of it.

There are two main restrictions for the static method. They are:

  • The static method can not use non static data member or call non-static method directly.
  • this and super cannot be used in static context.

Static block

  • Is used to initialize the static data member.
  • It is executed before main method at the time of classloading.
public class TryStaticBlock {
    static {
        System.out.println("This is a static block.");
    }

    public static void main(String[] args) {
        System.out.println("This is a main method.");
    }
}
This is a static block.
This is a main method.

相关文章

  • 2019-03-06 Java Interface

    Java static static 特点 static是一个修饰符,用于修饰成员。 static修饰的成员被所有...

  • 2019-03-07 Java static

    Java static static 特点 static是一个修饰符,用于修饰成员。 static修饰的成员被所有...

  • Test

    ```java public static void test() {} ```

  • 无标题文章

    ```java public static void main() { ```

  • 我的乌龟流星雨

    import java.awt.*; public class java{ public static void ...

  • Java 面向对象2

    Java 面向对象 1. static 和 final static关键字---修饰成员变量 用static修饰的...

  • 1.3 static 关键字

    static 关键字 《Java编程思想》 中:static方法就是没有this的方法 static 修饰的方法或...

  • Java static

    Static variable In this above example, we have created an...

  • Java static

    在总结完 想知道Java代码执行顺序么,Let's go 一文后,对 Java 中的static关键字有了更深的理...

  • Java static

    参考http://www.cnblogs.com/chenssy/p/3386721.html http://de...

网友评论

    本文标题:Java static

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