Java Static Object Statics

Source: Internet
Author: User

What is a static variable

As we all know, we can create multiple objects of this class based on a class, each with its own members and independent of each other.

At some point, however, we would prefer all objects of the class to share the same member. This is the time for static!!

Members in Java that are decorated statically are called static members or class members. It is owned by the entire class, not by an object, which is shared by all objects of the class.

Static members can be accessed directly by using the class name or by using the object name. Of course, given the particularity of his role, it is more recommended to access the class name ~ ~

Use static to modify variables, methods, and blocks of code.

 Public classTest1 {//static variables are statically modified, and objects of all classes are shared hobby    StaticString hobby = "tedu";  Public Static voidMain (string[] args) {//static variables can be accessed directly using the class name without creating an objectSYSTEM.OUT.PRINTLN ("Access Hobby by class name:" +test1.hobby); //to create an object of a classTest1 T1 =NewTest1 (); //using object names to access static variablesSystem.out.println ("Use object name to visit hobby:" +t1.hobby); //Modify the value of a static variable in the form of an object nameT1.hobby = "CJJ"; //use the class name again to access the static white variable, the value has been modifiedSYSTEM.OUT.PRINTLN ("Access Hobby by class name:" +test1.hobby); }}

Operation Result:

Access hobby:tedu by class name use object name visit hobby:tedu access by class name HOBBY:CJJ
Note:

Static members belong to the entire class, and when the system first uses the class, it allocates memory space until the class is unloaded for resource recycling! ~~

static method used by static

As with static variables, we can also use the static modification method, called a static method or a class method.

In fact, the main method that we've been writing about is the static method.

 Public classTest01 {//declaring a static method with the static keyword     Public Static voidprint () {System.out.println ("Welcome you: cjj!"); }         Public Static voidMain (string[] args) {//calling a static method directly using the class nameTest01.print (); //It can also be called by object name, and it is recommended to use the class nameTest01 T1 =NewTest01 ();    T1.print (); }}

Operation Result:

Welcome you: cjj! welcome you: CJJ!
Attention:

1 static members can be called directly in a static method, but not non-static members directly. such as:

if you want to invoke a non-static variable in a static method, you can access the non-static variable by creating the object of the class and then through the object . Such as:

2, in the ordinary member method, you can directly access the same kind of non-static variables and static variables , such as:

3, static methods can not directly invoke non-static methods, you need to access the non-static method through the object. such as:

 Public classTest01 {String name= "CJJ";//Static member variables    StaticString hobby = "study";//non-static member variable//Common member Methods     Public voidprint1 () {System.out.println ("Normal Member Method ~ ~ ~"); }        //Static member Methods     Public Static voidPrint2 () {System.out.println ("Static Member Method ~ ~ ~"); }         Public Static voidMain (string[] args) {//The normal member method must be called by the object.Test01 T1 =NewTest01 ();        T1.print1 (); //static methods can be called directlyPrint2 (); }}
Static initialization block for 3.static use

In Java, data can be assigned by initializing blocks. Such as:

 Public class Test02 {    //  define a member variable     / / Assign a value to a member variable by initializing the block     {        = "CJJ";    }}

Execute the code block in turn. If you initialize a block with the static adornment, it is called a static initialization block.

Attention:

static initialization blocks are executed only once when the class is loaded, and static initialization blocks can only assign values to static variables and cannot initialize normal member variables .

Let's look at a piece of code:

 Public classTest02 {intNUM1; intnum2; Static intnum3;
PublicTest02 () {//construction method num1= 11; System.out.println ("Assigning a value to a variable num1 by constructing a method"); }
{ //Initialize blocknum2 = 22; System.out.println ("Assigning values to variable num2 by initializing blocks"); }
Static{// static initialization block num3= 33; System.out.println ("Assign a static variable num3 by static initialization block"); } Public Static voidMain (string[] args) {Test02 T1=NewTest02 ();//Creating ObjectsSystem.out.println ("NUM1:" +t1.num1); System.out.println ("Num2:" +t1.num2); System.out.println ("NUM3:" +num3); }}

Operation Result:

Assigning a value to a static variable num3 by initializing the block to the variable num2 by the constructor method assigns a value to the variable num1 num1:num2: 33

by outputting The result, we can see that the static initialization block is executed first when the program is run, then the normal initialization block is executed, and the construction method is finally 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.

Java Static Object Statics

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.