[Java Study Notes] java language core-object-oriented static keywords, learning notes static

Source: Internet
Author: User

[Java Study Notes] java language core-object-oriented static keywords, learning notes static

  • Static keyword usage
  • Modified members have the following features:
    • Usage notes
      • Differences between instance variables (member variables) and class variables (static variables)

      Member variables exist with the creation of objects and are released with the collection of objects;

      Static variables exist as the class is loaded and disappear as the class disappears.

      2. Different call Methods

      Member variables can only be called by objects
      Static variables can be called by objects or by class names (class names are generally recommended)

      3. different aliases

      A member variable is also called an instance variable.
      Static variables are also called class variables.

      4. Different data storage locations

      The member variable data is stored in the object in the heap memory, so it is the special data of the object.
      Static variable data is stored in the static area of the Method Area (data area or shared data area), so it is also called the shared data of objects.

      • Memory Diagram

      Run the following code in the memory:

      class Person{    private String name;    private int age;    static String country = "CN";    public Person(String name, int age){        this.name = name;        this.age = age;    }    public void show(){        System.out.println(Person.country+":"+this.name+":"+this.age);    }    public static void method(){        System.out.println(Person.country);    }}class StaticDemo2{    public static void main(String[] args){        Person.method();                Person p = new Person("java",20);        p.show();    }}

      Memory diagram:

      Note:

      • Static code block
      Static {System. out. println ("this is a static code block ");}

       

      1. Static code block

      It is executed once as the class is loaded. Class.

      2. Role

      Class initialization. For example, all the Math classes provided by Java APIs are static methods and object creation is not allowed. At this time, constructors cannot be used for initialization, because constructors generally initialize objects, objects cannot be created. Therefore, static code blocks must be used for initialization.

      • Construct code blocks

      Unlike static code blocks, a code block belongs to a class, for example:

      Class Demo {System. out. println ("this is the Construction Code block ");}
          //。。。}

      Related to objects

      All objects can be initialized. constructors initialize the objects accordingly.

      It is executed every time an object is created. Static code blocks are executed once during class loading.

       

      Contact Us

      The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

      If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

      A Free Trial That Lets You Build Big!

      Start building with 50+ products and up to 12 months usage for Elastic Compute Service

      • Sales Support

        1 on 1 presale consultation

      • After-Sales Support

        24/7 Technical Support 6 Free Tickets per Quarter Faster Response

      • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.