Java essay: Static keyword

Source: Internet
Author: User

First, Introduction.

/**************************************************************thinking in java********************************* ***********************************/

In general, when you create a class, it is the appearance and behavior of the object that describes that class. Unless you create the object of that class with new, you do not actually get any objects. When you execute new to create an object, the data storage space is allocated and its methods are called by the outside world.

There are two scenarios that cannot be solved with the above method. One scenario is that you just want to allocate a single storage space for a particular domain, without thinking about how many objects to create, or even creating any objects at all. Another scenario is that you want a method not to be associated with any of the objects that contain its containing classes. That is, the method can be called even if the object is not created.

These two requirements can be met through the static keyword. When declaring a thing to be static, it means that the field or method is not associated with any object of the class that contains it. So, even if you never create any objects of a class, you can call its static method or access its static domain. Typically, you must create an object and use it to access data or methods. Because non-static domains and methods must know the specific objects they work on. /* (You do not need to create any objects before using the static method, so for the static method, you cannot simply call another non-static domain or method without specifying a named object.) To directly access a non-static domain or method (because a non-static domain or method must be associated with a particular object). */

  Some object-oriented languages employ the two terms of class data and class methods, which represent that data and methods exist only as an entire class, not a particular object of a class.

/**************************************************************thinking in java********************************* ***********************************/

Second, enter the static world.

1. Static domain

1 class employee{2     Private Static int nextid=1; 3     Private int ID; 4 }

Each object has its own ID field, but all instances of this class share a NextID domain. 1000 an Employee class object, there are 1000 instance domain IDs. But there's only one nextid. even if there are no objects, static domain NextID also exists . it belongs to a class and does not belong to any independent object .

2. Static constants

Static constants use less, but static constants are used more. For example, a static constant is defined in the Math class:

1  Public class math{2      Public Static Final Double pi=3.1415926535; 3 }

In the program, this variable can be obtained in the form of Math.PI.

Another static constant that is used more than once is System.out. It is declared in the system class:

1  Public class system{2      Public Static Final PrintStream out= ...; 3 }

3. Static methods

A static method is a method that cannot be manipulated against an object. For example, the Pow method of the math class is a static method. Static methods are methods that do not have this parameter.

A static method cannot manipulate an object, so the instance domain cannot be accessed in a static method. However, a static method can access a static domain in its own class.

4. Factory method

Static methods also have

Java essay: Static keyword

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.