Object-oriented _static keywords

Source: Internet
Author: User

First, static overview:
1. is a modifier, static modifier, used to decorate members (member variables and member functions) can not decorate the local
2. When a member is modified, there is a way to call, in addition to being called by the object can be called directly by the class name usage is: class name. static members
3. Statically decorated properties are stored in the method area (methods in the Class), in the shared area, in the data area. This can save memory
4. Extract the commonality of each object and use the static modifier to store the properties of that commonality in the method area so that you can save memory
5. Unique properties of each object do not use static decoration, exist in the object

6. Decorate a property with the static keyword (a variable declared static is essentially a global variable)

7. Modifying a method with the static keyword typically defines a method as static in a ray, which means that the method can be called without an object of this class

second, the characteristics of static:

1. Load with the load of the class, meaning that as the class appears, it is loaded into memory and disappears as the class disappears, indicating that his life cycle is the longest,

2. Precedence over the existence of objects, that is, static first existence, the object is after the existence of

3. Shared by all objects

4. Can be called directly by the class name.

Third, static use precautions:
1, static methods can only access static members, cannot access non-static, because static precedence over the existence of the object, no object when the non-static member does not exist. Non-static methods can access both static and non-static (static properties and methods are stored in the method area memory after the class is loaded, and no objects are produced at this time, and normal methods and properties belong to the object, so static methods cannot call non-static methods)
2. The This,super keyword cannot be defined in a static method. Because static takes precedence over the existence of an object, this is not available in a static method

Iv. Static Advantages and disadvantages:
Benefit: Save space by storing the shared data of the object in a separate space. It is not necessary to store a point in each object can be called directly by the class name.
Disadvantages: too long life cycle, access limitations, (static although good access to static)

The difference between instance variables (member variables) and class variables (static variables):

1. Storage location
Class variables are present in the method area (data area, shared area) as the class is loaded and disappear as the class disappears
Instance variables exist in heap memory as objects are established
2. Life cycle
The life cycle of a class variable is the longest and disappears as the class disappears
The life cycle of an instance variable disappears as the object disappears (the life cycle of the class is the longest, followed by the object)
3. Invocation mode
Member variables can only be called by an object
Static variables can be called with an object and can be called with a class name

Six, code examples:

1  Public classStaticdemo {2      Public Static voidMain (string[] args) {3Person p=NewPerson ();4System.out.println (P.getcountry ());//invoking a member through an object5Person p1=NewPerson ();6 p1.show ();7 p.show ();8System.out.println (Person.getcountry ());//The member is invoked through the class name. When a member is decorated, there is a way to call, in addition to being called by the object can be called directly by the class name usage is: class name. static members9     }Ten } One classperson{ A     PrivateString name;//member variables, instance variables, as objects are built in heap memory, and the life cycle disappears as the object disappears -     Private StaticString country= "CN";//The commonality of each object is extracted with static modification, stored in another space so that it is not necessary to create a space for each object for this property, which can be very memory-intensive -      the      PublicString GetName () { -         returnname; -     } -  +      Public voidsetName (String name) { -          This. Name =name; +     } A  at      Public StaticString getcountry () { -         returnCountry; -     } -  -      Public Static voidSetcountry (String country) { -Person.country =Country; in     } -  to     //static member variables, class variables exist in the method area, the longest life cycle, +      Public voidShow () { -System.out.println (name+ "..." +country); the     } *}
 Public classninetyeight_ninetynine_onehundred { Public Static voidMain (string[] args) {Mistress M1=NewMistress ("Little White"); Mistress m2=NewMistress ("Little Red");        M1.desc ();        M2.desc (); M1.profession= "small Three";//Get rid of the static properties in the method areaM1.desc ();                M2.desc (); //mistress.profession;//static data does not belong to the object, it belongs to the class, so calling static data uses the class area call//calling a static method (directly called by the class name)mistress.promosion ();        M1.desc ();    M2.desc (); }}classmistress{String name; StaticString profession = "Lover";//using the static modifier property, this property is present in the method area     PublicMistress (String name) { This. Name =name; }         Public voiddesc () {System.out.println ("My name is:" +name+ ", My occupation is:" +profession); }            //use static to modify a method (indicating that the method is a static method, not an object, called directly with the class name)     Public Static voidpromosion () {System.out.println ("Positive."); Profession= "Housewife"; //desc (); Static methods cannot invoke non-static methods because the Desc method is an object, and the static method loads the object when it is not yet created .    }}

Vii. when to use static

1. When to define static variables (class variables):

When shared data is present in an object, the data is decorated statically,

The unique data in the object to be defined as non-static exists in heap memory.

2. When to define a static function

When there is no access to non-static data (unique data for the object) within the feature. Then the function can be defined as static (can also be defined as non-static, but non-static needs to be called by the object, but only to create the object call non-static, no access to the unique data method, the object creation is meaningless, so for the program more rigorous, still defined as static)

Object-oriented _static keywords

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.