Java code block static, non-static

Source: Internet
Author: User

The memory partition of the Java Virtual machine: Java stack, heap, method area, local method stack, PC register. There is also the concept of a constant pool, where a virtual opportunity assigns a constant pool to each type, rather than an instance.

For example, there is a class with many subclasses, so the final variable defined in the parent class cannot be changed in the subclass. It can be stated that it is assigned on a type.

A variable or method that is modified by static belongs to the class itself, not to an instance of the class. It was initialized when the class was loaded into the virtual machine, he was shared with the method area, the method area, and the heap area, so he was designed to be thread-safe. Assuming that two threads are attempting to access a class named a, and that the class has not been transferred to the virtual machine, there should only be one thread to load it, while the other thread can wait. So why we can say that static variables are defined as shared data.

Let's take two examples to see their differences.

 Public Interface Angry {    = "Girl!!!" ;         int angrylevel = dog.getangrylevel ();}
 public  class   Dog { static  final                 String greeting = "Woof,woof" ;  public   Dog () {System.out.println (    span> "DOg" );  static  {System.out.println (    /span> "Dog is in" );  static  int   Getangrylevel () {System.out.println ( "Angry is in"  return  1; }}
 Public class Example1 {        publicstaticvoid  main (string[] args) {        System.out.println (angry.greeting);        System.out.println (dog.greeting);         // new Dog ();             }    static  {        System.out.println ("EXAMPL1");}    }

The results of this output are:

Exampl1
Girl!!!
Woof,woof

Open the Code of New Dog () and comment, and look at the results.

Exampl1
Girl!!!
Woof,woof
Dog is in
Eof

Then take a look at the following code:

 Public class a{    staticint A = ten;         Static {        System.out.println ("Initialize a");}    }
 Public class extends a{    staticint b = 5;     Static {        System.out.println ("Initialize B");}    }
 Public class example2{    publicstaticvoid  main (string[] args) {        int C = b.b;        System.out.println (c);    }     Static {        System.out.println ("Initialize Example2");}    }

The result of his output is:

Initialize Example1

Initialize a

5

At first I always thought that when I called a method or variable of this class, I would execute a static block of code or a non-static block of code. See the "Deep Java Virtual machine" after understanding a lot.

Summary below:

The first example: None of the 3 classes has an inheritance relationship. The variables defined by the interface are public static final by default, and the default method is public abstract.

The Main method is the entry of the program, the virtual opportunity initializes the class of the thread, then he executes the contents of the static code block, then the program executes from top to bottom, executes angry.greeting, prints his value, executes dog.greeting, prints his value. Angry and dog are not instantiated. So the method of static code block is not called.

New a Dog object, when the program actively instantiates an object, then he calls the static code block of dog, non-static code block, parameterless constructor.

The second example: Class B Inherits Class A. There is no active creation of Class B objects in Example2, but the program calls static variables of Class B, and Class B inherits Class A, and the virtual opportunity loads and instantiates Class A (the parent of B), so the static code block of Class A, non-static code block, and parameterless constructor are executed.

But he did not execute the static block of Class B, because the program did not instantiate Class B objects actively.

There are six scenarios that belong to the active use of initialization.

1. When creating a new instance of a class (new, clone, deserialization, reflection)

2, call the static method.

3. Use a static field to a class or interface, or assign a value to the field, except for a static field that is final decorated, which is initialized to a compile-time constant expression.

4, when initializing a subclass of a class.

5. When using the reflection method in class.

6. Start the class with the main method.

Java code block static, non-static

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.