Mastering the definition and use of three blocks of code in Java

Source: Internet
Author: User

Mastering the definition and use of three blocks of code in Java


code block Concepts

The code block itself is not a very difficult concept to understand, in fact, it has been used before. The so-called code block refers to the use of "{}" in a piece of code, depending on the location, the code block can be divided into four kinds: ordinary code block, building block, static code block, synchronous code block, where the synchronization code block this book will be in the multi-threaded part of the explanation, this chapter first to observe the other three blocks

Common code blocks
a block of code that is directly defined in a method is called a normal code block.

Public class codedemo01{
Public static void Main (String args[]) {
{//Normal code block
int x = 30;//It belongs to a local variable
System.out.println ("Normal code block--X =" + x);
  }
int x = 100; Same as local variable name
System.out.println ("Outside the code block-x =" + x);
 }
};

Operation Result:

C:\Documents and settings\administrator\ desktop \java>java CodeDemo01
Normal code block--X =
code block away--X =

Building Blocks:
defining a block of code directly in a class is called constructing a code block.

class demo{
{//write code blocks directly in the class, called Building blocks
System.out.println ("1, building block. ") ;
 }
Public Demo () {//define construction method
System.out.println ("2, construction method. ") ;
 }
};
Public class codedemo02{
Public static void Main (String args[]) {
new Demo (); Instantiating an object
new Demo (); Instantiating an object
new Demo (); Instantiating an object
 }
};

Operation Result:

C:\Documents and settings\administrator\ desktop \java>java CodeDemo02
1, building blocks.
2, the construction method.
1, building blocks.
2, the construction method.
1, building blocks.
2, the construction method.

The construction block takes precedence over the construction method execution and executes multiple times, executing the contents of the construction block whenever an instantiated object is produced.

Static code block:
using the static keyword declaration directly is called a static code block.

class demo{
{//write code blocks directly in the class, called Building blocks
System.out.println ("1, building block. ") ;
 }
static{//using static, referred to as block code
System.out.println ("0, Static code block");
 }
Public Demo () {//define construction method
System.out.println ("2, construction method. ") ;
 }
};
Public class codedemo03{
static{//defining static blocks in the class where the Main method resides
System.out.println ("code block defined in the class where the Main method resides");
 }
Public static void Main (String args[]) {
new Demo (); Instantiating an object
new Demo (); Instantiating an object
new Demo (); Instantiating an object
 }
};

Operation Result:

C:\Documents and settings\administrator\ desktop \java>java CodeDemo03
blocks of code defined in the class where the Main method resides
0. Static code block
1, building blocks.
2, the construction method.
1, building blocks.
2, the construction method.
1, building blocks.
2, the construction method.

Static blocks take precedence over the Main method execution, and if static blocks defined in the normal class take precedence over the construction block execution, no matter how many instantiation objects are produced, the static code block executes only once, and the primary function of the static code block is to initialize the static.

Mastering the definition and use of three blocks of code 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.