Getting started with JavaSE 12: static usage of Java surface objects

Source: Internet
Author: User

Getting started with JavaSE 12: static usage of Java surface objects

We can create multiple objects of this Class Based on a class. Each object has its own members and is independent of each other. However, in some cases, we prefer

All objects in this class share the same member. Now it's time for static to show its strength.

The modified members in Java are called static members or class members. It belongs to the whole class, not to a certain object.

Objects are shared. Static members can use class names for direct access or object names for access. Of course, we recommend that you use

Class Name access.

You can use static modifiers to modify variables, methods, and code blocks.

I. static usage of static variables in Java

Static modifier variables are called static variables or class variables. All class objects share static variables. We can use the class name

You can directly access static variables, or use instance objects of the class to access static variables.

For example, we define a static variable holobby in the class. The operation code is as follows:

Running result:

Note the following when using static variables:

A static member belongs to the entire class. When the system uses this class for the first time, it allocates memory space for it until the class is detached.

B static member variable is a global variable of the entire class. It does not belong to a specific object. A common member variable is an object in the class and cannot be

All objects are shared.

C static member variables must be defined globally. Since normal member variables can be initialized when an object is created

You do not need to define it separately. You only need to declare it.

D. The static member variable exists before there is no object.

Static Method in Java

Like static variables, we can also use static modifiers to modify methods, called static methods or class methods. In fact, the main

The method is static. The use of static methods is as follows:

Running result:

Note the following when using static methods:

A can directly call static members of the same type, but cannot directly call non-static members. For example:

B. Call non-static variables in static methods. You can create class objects and access non-static variables through objects. For example:

C In the common member method, you can directly access non-static and static variables of the same type, as shown below:

D. You cannot directly call non-static methods in static methods. You must use objects to access non-static methods. For example:

Iii. static usage in Java-static initialization Block

In Java, values can be assigned through initialization blocks. For example:

The class declaration can contain multiple initialization blocks. When a class instance is created, these code blocks are executed in sequence. If you use the static modifier

Modifying the initialization block is called a static initialization block.

Precautions for using static initialization blocks include:

Static initialization blocks are executed only when the class is loaded and only once. At the same time, static initialization blocks can only assign values to static variables, no

Initialize common member variables.

Let's look at a piece of code:

Running result:

Through the output result, we can see that the static initialization block is first executed when the program is running, then the normal initialization block is executed, and finally the construction is executed.

Method. Because the static initialization block is only executed once during class loading, the static initialization block is not executed when the object hello2 is created again.

Let's look at an example:

Public class Test {String name; // declare the variable name String sex; // declare the variable sex static int age; // declare the static variable age // construct the public Test () {System. out. println ("initialize name through constructor"); name = "tom" ;}// initialize the block {System. out. println ("initialize sex through initialization block"); sex = "male";} // static initialization block static {System. out. println ("initialize age through static initialization block"); age = 20;} public void show () {System. out. println ("name:" + name + ", Gender:" + sex + ", age:" + age);} public static void main (String [] args) {// create the object Test = new test (); // call the show method Test of the object. show ();}}
Running result:

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.