Static code blocks and construction code blocks in Java

Source: Internet
Author: User

In a recent review of some of the knowledge in Java, here is a way to learn about static blocks of code and construct blocks of code.

First of all, the code is balabala some definitions Ah, assignment Ah, the operation of some Java code, surrounded by two curly braces, it becomes a magic block of code

{        a=3;} static{        b=4;}

Just like the one above.

The parentheses add static to the code block, you remember oh, as long as add static this thing only after, the east is static, and can only operate static stuff.

such as static methods, static variables, static pointers, of course, there is no pointer in Java.

Then I used a small code to understand the two concepts, on the code

Class Man{private String name = "X";p rivate int age;private static Boolean isman;p ublic void Showman () {System.out.println (name); System.out.println (age);} public static void Showmanstatic () {System.out.println (Isman);} {//code block 1system.out.println ("--------Block 1--------"); age = 10;showman ();} Public String GetName () {return name;} public int getage () {return age;} Man () {System.out.println ("creator"); name = "Seeee";}  static {//code block, only static member IsManSystem.out.println ("I am Static code block 1");//showman (); This is important, showman is a normal member function, not static, so you can not use showmanstatic (); This can be Oh System.out.println ("Isman" +isman); Isman = true;} {//code block 2system.out.println ("--------Block 2--------"); age = 12;showman ();} {//code block 3system.out.println ("--------block 3--------"); age = 13;showman ();} static {//code block 2SYSTEM.OUT.PRINTLN ("I am Static code block 2");}} Class Test{public static void Main (string[] args) {man m = new Man (); System.out.println (M.getname ()); System.out.println (M.getage ());}} Here's the result I'm static code block 1falseisManfalse I'm quietState code block 2--------Block 1--------X10--------Block 2--------X12--------block 3--------x13creatorseeee13 

Magically, the static code block is called first, and only static methods and static member variables can be called, remember, this heap of things is static OH.

Then the ordinary code block, presumably because it is often used for object initialization, it is also called the Construction code block, and the order is the same as the upper and lower order, 1 to 2 to 3, static is the same, and finally began to construct the object.

This thing understand, in fact, I do not know, if the great God know, can tell the younger brother.

Static code blocks and construction code blocks in Java

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.