Introduction to the use of static keywords in Java

Source: Internet
Author: User

1           Static member variables

variables defined in a class are often referred to as member variables if the modifier is added to the front of a member variable Static , then we call this member variable a static member variable.

1.1 methods for defining member variables and static member variables

Create a car color One is a static member variable mycolor

Public class car {         public string  color;//General member variables          public static  stringmycolor;//static member Variable          void show () {                     SYSTEM.OUT.PRINTLN ("The color of the car is" +this.color);                   SYSTEM.OUT.PRINTLN ("The color of my car is" +this.mycolor);          }public static void main (String[]args)  {                    carcar=new  car ();                    car.show ();           }} 

The result of the operation is:

the color of the car is NULL

The color of my car is NULL

whether a member variable or a static member variable has no custom initialization, the default initial value given by the system is NULL . At this point, the member variable and the static member variable are the same.

1.2 The use of static member variables

The value of the 1.2.1 member variable is exactly the same in different objects

A class can create multiple object instances, each of which creates an object that allocates an appropriate piece of space in memory to store member variables and methods in the object. If the value of a member variable in each object is the same, then for this variable, each object is also allocated memory space for storage, resulting in a waste of memory space. As shown, the address in the figure is demo, not the actual memory address. Same below

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/82/FD/wKiom1dn3ZzAsVGaAABx7pf-aII884.jpg "title=" static modifier static.jpg "alt=" Wkiom1dn3zzasvgaaabx7pf-aii884.jpg "/>

Figure 1 usage graph of memory addresses when defining normal member variables

To solve the waste of memory space caused by using member variables, you can define static member variables to address the waste of this memory space. A variable that is defined as a static member variable allocates a memory address that is a shared data memory address, is not in the same region as the memory address allocated by the object, and is allocated only once, and is not duplicated because of the object's build. Once the build object is complete, static member variables can also be accessed through the object, achieving the same effect as accessing ordinary member variables through the object. Using static member variables greatly reduces the amount of wasted memory space, as shown in.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M01/82/FC/wKioL1dn3d2BJO2KAAB2iK8ILWs421.jpg "title=" static modifier static2.jpg "alt=" Wkiol1dn3d2bjo2kaab2ik8ilws421.jpg "/>

Figure 2 usage graph of memory addresses after defining static member variables

The 1.2.2 class assigns a value to a variable during loading

There is a variable that expects this variable to be assigned to it during the initialization of the class, and how do you accomplish such a function?

We know that the initialization of an ordinary member variable requires the object of the class to be constructed first. You can assign a member variable only if the object of the class exists. So the normal member variables do not fulfill the requirements we put forward earlier.

So how do you accomplish this function? Static member variables can be used here. Because static member variables are loaded with the load of the class, that is, when the class is defined, memory is allocated to the class while the static member variable is allocated memory. So after the class is loaded, the static member variable is loaded at the same time. Without building objects, you can access static member variables directly.

public class Car {publicstatic String mycolor= ' white ';         publicstatic void Main (string[] args) {System.out.println ("My Car Color is" +mycolor); }}

The result of the operation is:

The color of my car is white

from the results of the run, you can find objects that do not build classes, as well as access static member variables. This solves the problem we have raised.

we use the class name to access static member variables, so what if we use objects to access static member variables? Let's modify it according to the code below.

public class Car {publicstatic String mycolor= ' white ';      publicstatic void Main (string[] args) {car car1=new car ();                   System.out.println ("My first car color is" +mycolor);         System.out.println ("My Car Color is" +mycolor); }}

The result of the operation is:

the color of my first car is white.

the color of my car is white

from the running result we find that the static member variable can be accessed not only through the class, but also through the object.

1.2.3 static member variables differ from member variables

Static member variables and non-static member variables are member variables, but there are differences, mainly in the following points:

1, the storage address is different, the static member variable is stored in the data sharing area, the member variable is stored in the object memory area;

2, the generated time is different, static member variables exist at the time of the class generation, and the member variables need to build the object before it exists.

3, different calls, static member variables can be called by the class can also be called by the object, and member variables can only be called by the object.


This article is from "Rookie Advanced" blog, please be sure to keep this source http://zyejl.blog.51cto.com/2769851/1791150

Introduction to how static keywords are used 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.