Java static statics

Source: Internet
Author: User

Static: Statics
Usage: is a modifier used to decorate members (member variables and member functions)
When a member is statically decorated, it is called by the class name, in addition to being called by the object.
Invocation form: class name. static member
Class name. static function

The characteristics of static:
1, loaded as the class loads.
In other words: Static disappears as the class disappears. Indicates that it has the longest life cycle.
2, precedence over the existence of the object
Be clear: Static is the first existence, the object is after the existence.
3, static members or static variables are shared by all objects
4, can be called directly by the class name

Instance variables: Also called member variables
Class variables: Static member variables, also called static variables


The difference between instance variables (member variables) and class variables (static variables):
1, different storage locations
Member variables exist in heap memory as the object is established;
Static variables exist in the method area as the class is loaded, and the method area is also known as the shared area and data area.
2, Different life cycle
The lifetime of a class variable is the longest, loaded as the class is loaded, and disappears as the class disappears.
The life cycle of a member variable disappears as the object disappears.


Static Use precautions:
1, static methods can only access static members, because the static generation of relatively early, the object produced relatively late, non-static variables with the object to produce;
Non-static methods can access both static and non-static. However, when you determine that an object is already established, you can access the members and methods of the object.
2, the This,super keyword cannot be defined in a static method.
Because static overrides the existence of an object, and the This and Super keywords represent an object, the this and super keywords cannot appear in a static method.
3, the main function is static.


Static pros and Cons:
Li: The shared data of the object is stored in a separate space, saving space, and it is not necessary to store one copy of each object; it can be called directly by the class name.
Cons: The life cycle is too long, access is localized (local though good, can only access static.) )
Class Person {string Name;static string country= "CN";p ublic static void Show () {System.out.println (country+ "::::");}} Class Staticdemo{public static void Main (string[] args) {//system.out.println (person.country);//person.show (); person P1 = new person (); System.out.println (P1.name);}}

Java static 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.