A detailed explanation of static code blocks used in Java _java

Source: Internet
Author: User

I. Comparison with static methods

In general, if some code has to be executed at the time the project is started, you need to use static code blocks, which are active, need to be initialized at the time the project is started, and when other programs are called without creating the object, the static method is required when the class loads is already loaded can be called directly with the class name such as the main method must be static this is the program entry. The difference between the two is that static code blocks are executed automatically; Static methods are executed when they are invoked.


Two. static method considerations

When using a static method for a class, note:

A. In a static method, you can only invoke other static members (including variables and methods) directly in the same class, but you cannot directly access Non-static members in your classes. This is because, for non-static methods and variables, you need to create an instance object of the class before you can use it, and the static method before using

You do not have to create any objects.

B. Static methods cannot refer to the This and Super keywords in any way, because static methods do not create any instance objects before they are used, and when static method calls, the object referenced by this is not produced at all (the This keyword can only be used within the method, representing the to call method's

Object "is referenced).

Static variables are variables that belong to the entire class and are not part of an object. Note that variables in any method body cannot be declared static, for example: Fun () {static int i=0;//illegal. }

Three. Examples of procedures

Copy Code code as follows:

public class Teststaticcon {
public static int a = 0;

static {
A = 10;
System.out.println ("The static code block of the parent class is executing a=" + a);
}

{
A = 8;
System.out.println ("The Non-static code block of the parent class is executing a=" + a);
}

Public Teststaticcon () {
This ("A value in the method of constructing the parent class with parameters:" + teststaticcon.a); Invoke another construction method
System.out.println (a);
System.out.println ("Parent class parameterless construction method is executing a=" + a);
}

Public Teststaticcon (String N) {
SYSTEM.OUT.PRINTLN (n);
System.out.println (a);

}

public static void Main (string[] args) {
Teststaticcon TSC = null;
System.out.println ("!!!!!!!!!!!!!!!!!!!!!");
TSC = new Teststaticcon ();
}
}

Copy Code code as follows:

Run Result:
The static code block of the parent class is executing a=10
!!!!!!!!!!!!!!!!!!!!!
Non-static code blocks of the parent class are executing a=8
A value in the parent-class-with-parameter constructor method: 10
8
8
The parent class parameterless construction method executes a=8

Four. Users provide
Copy Code code as follows:

public class Staticblock {

static {
System.out.println ("static Block");
}
{
System.out.println ("Building block, defined in class");
}

Public Staticblock () {
System.out.println ("Construction Method Execution");
}

public static void Main (string[] args) {
New Staticblock ();
New Staticblock ();
}

}

Copy Code code as follows:

Static block
Building blocks, defined in the class
Construct method execution
Building blocks, defined in the class
Construct method execution

Conclusion: Static code blocks are executed automatically when the class is loaded, and the Non-static code block is the code that executes automatically when the object is created, and does not create an object that does not execute the class's non-static code block. And the execution order is a static code block------A non-static code block----a constructor.

One of the things that puzzles me is the value of "A in the parent-with-parameter construction method: 10", I wonder why it was not 8,debug at that time (F11, can not directly set breakpoints and then run, which is no different from direct operation), found first into the parameterless construction method, Executes the first statement and switches to another construction method (whether or not the first sentence is executed anyway, at this point A is still 10, the non-static code block has not been executed, the hint cannot find the source, whether or not the statement prompts this warning (not error, because the program continues to run normally), and then ran a non-static code block , and then proceed from the constructor method where the parameter is used ...

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.