The static of the Java learning Note

Source: Internet
Author: User

in Java. Static

Description: This is just my own learning Java personal experience and insight, share out, also considered their own notes. (I small white, have the wrong words hope the big God pointed out, do not like to spray. -.-)static variables

– In a Java class, you can use the static keyword to decorate a member variable, which is called a static variable

– Static variables are shared by all instances and can be accessed using the "class name. Variable name" form.

The –static keyword can only be used to decorate member variables, cannot be used to decorate local variables, or compile will error, the following demo is a violation of the syntax.

1  Public class student{2         Public void Study () {3         Static int num=10; // of the wrong 4 5         }67 }    

Static methods

--the method modified by the static keyword is called a static method

--like static variables, static methods can be accessed using the "class name. Method name" approach, or through the instance object of the class.

--a static method can only access members with static adornments, because there is no

A static decorated member needs to create an object before it can be accessed, and the statically modified method may not create any objects when called.

Static program block

– a block that is decorated with the static keyword is called a static program block

– When the class is loaded, the static code block executes, because the class is loaded only once, so the static code block executes only once

– In a program, you typically use static blocks of code to initialize member variables of a class

    • 1. What is a program block

In addition to member variables, methods, and construction methods, you can also use a program block, which is a block of statements included by a pair of curly braces ({}) .

    • 2 , the role of the program block

In Java, the function of the block is equivalent to completing some initialization operations, often in the program block implementation of the initialization of the member variables assigned values.

the following differences between normal and static program blocks are:

The execution is performed in a different order, and the static block is executed and executed once, while the normal block is invoked and executed once using the new operator to instantiate the object;

Static program blocks can access only class members, and ordinary blocks can access both instance members and class members.

The following starts the detailed explanation and the demo

Prerequisites introduced:
Data assignment in Java can be done through initialization blocks

Demo Description:

 Public class helloworld{    String name;    {    name= "Love You";    }}

In the declaration of a class, you can include multiple initialization blocks, which are executed sequentially when you create an instance of the class.
>> If you initialize a block with a static adornment, it is called a static initialization block.

It is important to note that static initialization blocks are only executed when the class is loaded and only once, while static initialization blocks can only assign values to static variables and cannot initialize normal member variables.
Demo:

 Public classstaticblock{intNUM1; intnum2; Static intnum3; //Construction Method     PublicStaticblock () {NUM1=91; System.out.println ("To complete NUM1 assignment with construction method"); }    //Initialize block{num2=74; System.out.println ("Completion of num2 assignment with initialization block"); }    //static initialization block static    Static{num3=85; System.out.println ("Complete the assignment of num3 with static initialization blocks"); }     Public Static voidMain (string[] args) {Staticblock sta1=NewStaticblock (); System.out.println ("NUM1:" +sta1.num1); System.out.println ("Num2:" +sta1.num2); System.out.println ("NUM3:" +num3); Staticblock Sta2=NewStaticblock (); }}

The result of the output is:

Why do you have this support output????

Did you notice the order of the outputs?

by outputting The result, we can see:

The static initialization block is executed first when the program is run.

Then execute the normal initialization block,

Finally, the constructor method is executed.

because static initialization blocks are only executed once when the class is loaded , static initialization blocks are not executed when the object Hello2 is created again.

The static of the Java learning Note

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.