Usage of static keywords in java

Source: Internet
Author: User
Tags instance method modifier

Static, so first, let's take a look at what static can modify:

The following test shows that static can be modified:

1. Statement Block

2. Member variables (but local variables cannot be modified)

3. Method

4. Interface (internal interface)

5. Class (only classes in the class can be modified, that is, static internal classes)

6. Static import added in jdk 1.5

So what does static modifier mean? When a class is created, a new type is created to describe the appearance and behavior of the class object. The data storage space is allocated only when the class object is created with new, the method is for external calls.

When a thing is declared as static, it means that the domain or method will not be associated with any object of the class containing it. That is to say, it does not depend on a specific instance of the class, shared by all instances of the class. As long as the class is loaded, the Java virtual machine can locate the class names in the method area of the runtime data zone. therefore, a static object can be accessed before any of its objects are created without referencing any objects...

1. static modifier statement block:

There can be multiple static {} statements in the declared order. When the JVM loads the class, the statement block is executed and only once.

2. static variables:

Static variables can be shared by class objects and have only one storage space. The JVM only allocates memory for the static state once, when the static variable memory is allocated during the loading process, the class name is used when the static variable is referenced. variable name is the preferred method, because it not only emphasizes that it is a static structure, but also provides a better opportunity for compiler optimization in some cases.

We recommend using the class name to directly reference the static variable. This clearly tells you whether the variable or attribute is static or modified.
But I want to talk about a more important knowledge point. Static is an important way to use a variable in a class. First, run the following code in the example:

The code is as follows: Copy code
Public class Person {
Static int [] arr = new int [3];
Public static void main (String [] args ){
Person per1 = new Person ();
Person per2 = new Person ();
Person per3 = new Person ();
System. out. println ("---- before changing -----");
System. out. print ("per1 --> ");
Per1.show ();
System. out. print ("per2 --> ");
Per2.show ();
System. out. print ("per3 --> ");
Per3.show ();
// Now I change its value
Person. arr [2] = 10; // This method is recommended.
Per1.arr [0] = 1; // This method is not generally used
System. out. println ("---- changed -----");
System. out. print ("per1 --> ");
Per1.show ();
System. out. print ("per2 --> ");
Per2.show ();
System. out. print ("per3 --> ");
Per3.show ();
  
 }
// Encapsulate a method for convenience to explain
Public void show (){
For (int I = 0; I <arr. length; I ++ ){
System. out. print (arr [I] + "t ");
  }
System. out. println ("");
 }
}

If it is an arr [] without static, we should understand it very well. If per1 is changed, the value of per2 is not affected.

 

Similar forms are also applied to methods that use static modifiers (called static methods ). It can be referenced by an object like a normal method, or it can be called in a special syntax, for example, "class name" + ". "+" method ", (); the method for defining static methods is the same as defining static variables:

 

  

The code is as follows: Copy code

Public class StaticTest2 {

 

Static void method () {("This is a static method ");}

 

   }

 

Common call method: StaticTest2 st = new StaticTest2 ();();

 

Because method is static (static), you can call the following method :();

 

3. static method:

The difference between static and non-static methods is that they can be accessed directly by class names. in the static method, non-static variables or methods cannot be referenced. However, you can pass a reference by passing parameters so that the reference can call non-static data.

4. static interface:

Inner interface is static by default, so static keyword addition and not addition are the same. In this example, the interfaces TestInInterface and TestInClass implement this interface. The following statements

The code is as follows: Copy code
StaticDescription. TestInInterface a = new StaticDescription. TestInClass ();
A. print ();

The same is true for removing the static keyword, but TestInterface and FF are still subject to access permission control.

5. static modifier class: (detailed description of the back part)

It indicates that it is a static internal class. When referencing external class objects, it must also be static and when a static internal class creates its own object, does not require the existence of peripheral classes. it is like the following:

Instance

The code is as follows: Copy code

Class Member {

Static int classVar = 0; // class variable

Int instanceVar; // instance variable

Member (int instanceVar ){

This. instanceVar = instanceVar;

 }

Static void setClassVar (int I ){

ClassVar = I;

InstanceVar = I; // The Class method cannot access the instance variable, but the class variable can only be used.

 }

Static int getClassVar () // class method
{Return classVar ;}

Void setInstanceVar (int I ){

ClassVar = I; // The instance method can be either an alias or an instance variable.

InstanceVar = I;

 }

Int getInstanceVar ()
{Return instanceVar ;}

}

Member not instantiated: after Member is instantiated:

For non-static data members, each class object has its own copy. Static data members are treated as class members. No matter how many objects are defined for this class, static data members also have only one copy in the program, which is shared by all objects of this type. That is to say, static data members are shared by all objects in the class. For multiple objects in this class, static data members only allocate memory once for all objects to share. Therefore, the values of static data members are the same for each object, and their values can be updated;

The code is as follows: Copy code

Member m1 = new Member ();

Member m2 = new Member ();

Memory allocation

There are two methods to reference static variables. As shown in classification, you can locate an object, such as m1.classVar;

It can also be referenced directly by its class name, such as Member. classVar, but not for non-static members.

Although static acts on a field market, it will definitely change the data creation method (because a static field has only one bucket for each class, A non-static field has a bucket for each object). However, if static is used for a method, the difference is not that big. An important usage of the static method is that it can be called without creating any object. This is important for defining the main () method, which is the entry point for running an application.

The static method is the method without this. Non-static methods cannot be called within the static method, but the opposite is acceptable. In addition, the static method can be called only by the class itself without creating any object. This is actually the main purpose of the static method. It is similar to a global method. Global methods are not allowed in Java, but you can access other static methods and static fields by setting the static method in the class.

Like any other method, the static method can create or use named objects of the same type. Therefore, the static method is usually used as the "shepherd" role, takes care of instances of the same type as its slaves.

However, if there are a lot of static methods in the code, you should reconsider your design.

 

 

Related Article

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.