Flexible use of the static keyword in Java classes

Source: Internet
Author: User
Tags value of pi

In professional terms, the storage space is created in the system only after the object is created with the new keyword. In some special cases, however, programmers may want to be able to allocate storage space for a particular member or call a method, and so on, without creating an object. Can you implement this requirement in Java?

First, do not establish objects that want to use the two actual situation.

Is the question that I mentioned to be unfounded? This is often the case in real-world application development. If you now need three objects, respectively, to calculate the area of the circle, the size of the ball, the volume of the cylinder. In the three-object approach, a constant called pi is needed, and the pi is fixed. Therefore, in program development, it is best to manage these common member variables in a unified way. When the precision of the member variable is adjusted, the precision of the result of the operation is adjusted. From a programmer's point of view, this is a requirement to allocate storage space to a member variable before an object is created, so that other classes can use that variable. This requirement cannot be achieved without the STATCI key words.

The second scenario is that a programmer might want a method to not be associated with any object of the class that contains it. This may be a bit of a mouthful to read. Simply put, the programmer can still invoke this method even if the object is not created with the new keyword. That is, when you create a class, you need to allocate storage space for that method. It's like when you build a car, you need to get the engine ready when you make a car design drawing. So even if the car has not been built, this start can be taken to see first. You also need to use the static keyword to complete this requirement.

Second, the use of static key words.

Simply put, the static keyword is used to allocate storage space to a particular member or method when creating a class. When a programmer adds a static keyword to a member variable or method, it means that the members and methods are not associated with any object instances of the class that contains it. In other words, the programmer can still invoke members or methods in the class, even if there is no object to create the class. Because they actually already exist, the system has allocated storage space for them. It is important to note, however, that the static keyword defines the members and methods that differ from the traditional method of object creation. So there is a difference between the definition and the specific reference process. The program developer must understand this discrepancy and pay attention to the actual work. Especially if programmers with other language development experience do not confuse this feature of the Java language with other languages, it is easy to make mistakes.

Use the static keyword to define a member variable.

As in the case above, you need to define a class, and then you need to use a member called PI in this class. If we name this member PI, the class can define the member variable in the following way.

Class circle{
   Static float pi=3.14

   }

The definition of this class is the biggest difference from other classes, which is to add a static keyword before the member variable. The member variable is set to STAITC state at this time. In fact, when you create this class, the system assigns it a storage space. Instead of creating the object when you create it. That is, the member variable is now separated from the class that contains it. In this case, if you use the New keyword to create an object, the member variable points to the same one. That is, if a programmer uses this class to create three objects, its member variable pi points to the same storage space. The value of pi for the member variable in the different objects is the same, because it actually refers to the same thing. Therefore, it is very easy to unify the values of member variables in multiple objects. Just add the keyword static to the front of the member variable when you define the class.

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.