Java understands instances and class members (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/11/30/2796085.html

This section describes how to create fields and instances of a class with the static keyword, instead of class instances.

 

Class variable

Create an object from the blueprint of the class. Each object has a unique copy of its own variables. For exampleBicycle class, the instance variable isCadence,Gear,Speed. EachEach variable of a bicycle object has its own value, which is stored in different memory locations.

Sometimes, you want all objects to share variables. This can be done through the static modifier. A field modified with the static keyword is called a static field or a class variable. These variables are associated through classes, rather than any object. Each instance of the Class shares each class variable, and each class variable is stored in a fixed position. Each object can modify the value of a class variable, but the class variable can also be manipulated without creating a class instance.

Example: addBicycle class, and assign a serial number. The first object starts from 1, and the ID of each object instance is unique. At the same time, you need a variable to track how manyBicycleOnly when the class has been created can you know the ID assigned to the next created object. This variable is not associated with other independent objects, but is related to the entire class. Therefore, we need a class variable,Numberofbicycles, As follows:

 Public   Class Bicycle {  Private   Int  Cadence;  Private   Int  Gear;  Private   Int  Speed;  //  Add an instance variable  //  The object ID      Private   Int ID;  //  Add a class variable for  //  Number of bicycle objects instantiated      Private   Static   Int Numberofbicycles = 0 ;...} 

Class variables can be referenced by class names, for example:

 
Bicycle. numberofbicycles

This usage clearly indicates that it is a class variable.

Note: You can also reference the static field through an object instance, for example

 
Mybike. numberofbicycles

But this is discouraged because it does not make it clear that they are class variables.

This method is not recommended because it cannot clearly indicate that it is a class variable.

We canIn the bicycle constructor, set the ID instance variable and auto-IncrementNumberofbicycles variables:

 Public   Class  Bicycle {  Private   Int  Cadence;  Private   Int  Gear;  Private   Int  Speed;  Private   Int  ID; Private   Static   Int Numberofbicycles = 0 ;  Public Bicycle ( Int Startcadence, Int Startspeed, Int  Startgear) {Gear = Startgear; Cadence = Startcadence; speed = Startspeed;  // Increment Number of bicycles  //  And assign ID number Id = ++ Numberofbicycles ;}  //  New Method to return the ID instance variable      Public   Int  GETID (){  Return  ID ;}...} 

 

Class Method

JavaProgramLanguage, supports static methods, just like static variables. Static method. It has a static modifier in the Declaration and should be called using the class name. You do not need to create any class instance, such

 
Classname. methodname (ARGs)

Note: You can reference static methods through objects, such

 
InstanceName. methodname (ARGs)

However, this method is not recommended because it cannot clearly express that it is a class method.

A common purpose is to use the static method to access static fields. For example, we can add a static method to the bicycle class to access static fields.Numberofbicycles.

 
Public Static IntGetnumberofbicycles (){ReturnNumberofbicycles ;}

The combination of variables and methods of instances and classes is not legal:

    • Instance methods can directly access instance variables and instance methods
    • Instance methods can be used to directly invoke class variables and class methods.
    • Class methods can directly invoke class variables and class methods
    • Class methods cannot directly access instance variables and instance methods-they must be accessed through object references. Similarly, class methods cannot use the this keyword because there is no instance reference currently.

 

Constant

Static modifier and final modifier combination, can be used to declare constants. The final keyword indicates that the field value cannot be initialized.

For example, the following Variable declares a constant named Pi, whose value is an approximate Pi (the ratio of the circumference of the circle with its diameter)

 
Static final double Pi = 3.141592653589793;

This method is used to declare constants. constants cannot be assigned again. If the program tries to do this, the compiler reports an error. By convention, the name of Changling is spelled in uppercase letters. If the name contains multiple words, the words are separated by underscores.

Note: If a native type or string is declared as a constant, its value can be obtained during compilation, and the compiler willCodeTo replace the constant. This is called the compile-time workload. If the value of a constant changes outside the class (for example, if the legislative pi is actually 3.975), you need to recompile any class that references this constant.

 

Bicycle class

Bicycle uses all the modifiers in this section. The Code is as follows:

 Public   Class  Bicycle { Private   Int  Cadence;  Private   Int  Gear;  Private   Int  Speed;  Private   Int  ID;  Private   Static   Int Numberofbicycles = 0 ; Public Bicycle ( Int  Startcadence,  Int  Startspeed,  Int  Startgear) {Gear = Startgear; Cadence = Startcadence; speed = Startspeed; ID = ++ Numberofbicycles ;}  Public   Int GETID (){  Return  ID ;}  Public   Static   Int  Getnumberofbicycles (){  Return  Numberofbicycles ;}  Public   Int  Getcadence (){  Return  Cadence ;}  Public  Void Setcadence ( Int  Newvalue) {Cadence = Newvalue ;}  Public   Int  Getgear (){  Return  Gear ;}  Public   Void Setgear ( Int  Newvalue) {Gear = Newvalue ;} Public   Int  Getspeed (){  Return  Speed ;}  Public   Void Applybrake ( Int  Decrement) {speed -= Decrement ;}  Public   Void Speedup ( Int  Increment) {speed + =Increment ;}} 

 

 


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.