The function and usage of static keywords in 5.JAVA basic review--java

Source: Internet
Author: User

Static keyword:
Characteristics:
1.static is a modifier that is used to decorate a member. Member variables (member variables, member functions) static modifiers are called static variables or class variables.
2.static decorated members are shared by all objects.
3.static takes precedence over the existence of an object because static members already exist as the class is loaded.
4.static decorated members have a way to call, can be directly called by the class name, (class name. static members).
The 5.static decorated data is shared data, and the data stored in the object is unique.

1     Private Static intAge//static variable of a member variable modified with static or called class variable2     PrivateString name;//member Variables3      Public Static voidShow () {//static functions can be called directly from a class4System.out.println ("Showstatic");5     }6      Public voidShowdemo () {//The member function needs to create an object to invoke the7System.out.println ("Showdemo");8}

The difference between a member variable and a static variable:
1. The different life cycle:
Member variables exist as objects are created and released as objects are reclaimed.
Static variables exist as the class is loaded and disappear as the class disappears.
2. The calling method is different:
Member variables can only be called by an object.
A static variable can be called by an object, or it can be called with a class name. (It is recommended to call with the class name)
3. Aliases are different:
Member variables are also known as instance variables.
A static variable is called a class variable.
4. Data storage locations are different:
Member variable data is stored in the object of the heap memory, so it is also called the object's unique data.
Static variable data is stored in the static area of the method area (shared data area), so it is also called the shared data of the object.

1  Public classDemo {2     Private Static intAge//static variable of a member variable modified with static or called class variable3     PrivateString name;//member Variables4      Public Static voidShow () {//static functions can be called directly from a class5System.out.println ("Showstatic");6     }7      Public voidShowdemo () {//The member function needs to create an object to invoke the8System.out.println ("Showdemo");9     }Ten      One      Public Static voidMain (string[] args) { ADemo.age = 1;//static variables can be called directly with the class name. Disappears as class loads and the class disappears -Demo d =NewDemo (); -D.age = 10;//A static variable can also be called by an object (it is recommended to call with a class name) theD.name= "Zhang San";//member variables can only be called through objects. As object creation exists, it is released as the object is reclaimed.  -Demo.show ();//static methods can also be called directly with the class name.  -D.show ();//static methods can also be called through objects.  -D.showdemo ();//A non-static method can only be called by an object.  +     } -}

Things to keep in mind when using static:
1. Static methods can only access static members. (Non-static access to both static and non-static)
2. The this or Super keyword cannot be used in a static method.
3. The main function is static.

1  Public classDemo {2     Private Static intAge//static variable of a member variable modified with static or called class variable3     PrivateString name;//member variables are also called instance variables4     5      Public Static voidShow () {//static Functions6Demo d =NewDemo ();//because static precedes object loading, you must create an object to access it if you need access7D.name = "Zhang San";//static methods cannot directly access non-static member variables8D.showdemo ();//static methods cannot directly access non-static member functions9 System.out.println (d.name);Ten     } One      Public voidShowdemo () {//member functions AAge = 10;//static variables can be accessed directly -Show ();//You can also access static functions directly - System.out.println (age); the     } -}

When to use static to decorate
1. Static variables:
The values of the member variables that are available in the Analysis object are the same. At this point, the member can be statically modified.
As long as the data in the object is different, is the object's unique data, must be stored in the object, non-static.
If the same data, the object does not need to make changes, only need to use, do not need to store in the object, is static.

2. Static functions.
Whether a function is statically decorated, it is a reference to whether the function has access to data specific to the object.
In short, from the source code, whether the function needs to access non-static member variables, if necessary, the function is non-static. If you do not need it, you can define the functionality as static. Of course, it can also be defined as non-static, but non-static needs to be called by the object, and only creating objects is meaningless.


Static code block:
Executes as the class loads, and executes only once.
Role:
Used to initialize a class.

1  Public classDemo {//If you want this class to be a static class and you need some parameters in the class to initialize, you need a static block of code.2     Private Static  intAge ;3     Private StaticString name;4     //omit Get, set method5     Static{//initialize a property when the class is loaded6Age = 10;7Name = "Zhang San"; 8     }9      Public Static voidShownoen () {//call this method The age is 10name for Zhang San (default value)TenSystem.out.println ("Age:" +age+ "Name:" +name); One     } A      Public Static voidShowintAge,string name) {//calling this method overrides the default value -Demo.age =Age ; -Demo.name =name; theSystem.out.println ("Age:" +demo.age+ "Name:" +demo.name); -     } -      Public Static voidMain (string[] args) { -Demo.shownoen ();//The result is: Age: 10 Name: Zhang San +Demo.show (50, "Zhao Si");//The result is: Age: 50 Name: Zhao Four -     } +}

The main function is special:
1. Format fixed.
2. Recognized and invoked by the JVM.
public static void Main (string[] args) {

}
Public: Because the permissions must be the largest.
STATIC:JVM does not require an object at the time of invocation, it is called directly with the class name to which the main function belongs.
void: The main function does not have a specific return value.
Main: The function name, not the keyword, is just a fixed name identified by the JVM.
String[] args: This is the argument list of the main function, is an array type parameter, and the yuan technique is a string type.

The function and usage of static keywords in 5.JAVA basic review--java

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.