Java object-oriented static keywords

Source: Internet
Author: User

Java object-oriented static keywords

We know that an object is created to generate an instance and encapsulate data. But when calling a function, it does not use the encapsulated data in these objects. Is it meaningful to create this object? (Although it can be compiled and run, it is a waste of space in the heap memory ).

Solution: it is not recommended to create an object. In Java, the static keyword is used. This is a member modifier. The static modifier can be called by objects, it can also be called by class name.

Static Method: for example:

 

Class Person {private String name; private int age; Person (String name, int age) {this. name = name; this. age = age;} public void speak () {System. out. println ("name =" + this. name + ", age =" + this. age); sleep () ;}// defines a sleep function. Public static void sleep () {System. out. println ("Sleeping zzzZZZZ") ;}} class PersonDemo {public static void main (String [] args) {// Person p = new Person ("lisi", 20 ); // p. speak (); // p. sleep (); Person. sleep ();}}

So static looks beautiful. Is it true that all methods are static? No! When defining a function, if the function does not require the member variables defined in the dimensions class (non-static), the function must be statically modified.

 

 

Static method features:

1. The static method cannot access non-static members, but the non-static method can access static members.

Note: the disadvantage of static access lies in its limitations. The advantage is that it can be called by class names.

2. this and super keywords are not allowed in static methods.

(Why ?) → Principle:

1. The static state is loaded with the loading of the class, and disappears with the disappearance of the class.

2. The static state takes precedence over the object existence and is shared by the object.

3. Because the static data first exists in the memory and cannot access the data in the later objects, the static data cannot be accessed.

In addition, this cannot be written internally, because the object may not exist at this time, and this has no point.

 

Static variables:

When do I define static variables?
When the value of the member variable is the same for each object, the member variable is statically modified.
 

Example (area of the circle ):

 

Class Circle {private double radius; // the radius of the Circle. Private static double pi = 3.14; // each circular object is stored in one copy, which is a waste of memory space. Share objects. Add static keyword modifier. Circle (double radius) {this. radius = radius;} // obtain the area of the Circle. Double getArea () {return radius * pi;} static void show () {System. out. println ("circle show run .. "+ pi) ;}} class CircleDemo {public static void main (String [] args) {Circle c = new Circle (3 );}}

 

Differences between static variables and member variables: 1. the scope is different.
Static variables belong to the class and member variables belong to objects.
Static variables are also called class variables, and member variables are also called instance variables.

2. Different calls.
Static variables can be called by objects and classes (generally called by class names)
Member variables can only be called by objects.

3. The loading period is different.
Static variables are loaded with the class.
Member variables are loaded with objects.

4. The memory storage areas are different.
Static variables are stored in the method area.
Member variables are stored in heap memory.

 

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.