Java Syntax: Final and static

Source: Internet
Author: User

Variables defined by final can be regarded as constants and cannot be changed;
Methods defined by final cannot be overwritten;
Classes defined by final cannot be inherited.
Final Static is added with the static feature.

Static and final are not directly related.

Static is to allocate an area in the memory for the whole class to be used. All class objects have their common values.

Differences between static and final
I. Static:
Under what circumstances should we use static?
1. You only want to use a storage area to save a specific data-no matter how many objects you want to create, or even not create an object at all.
2. We need a special method that is not associated with any objects of this class. That is to say, a method that can be called is required even if no object is created.
To meet these two requirements, you can use the static keyword.
Here is an example:

Once something is set to static, data or methods will not be associated with any object instance of that class. So even if you have never created an object of that class, you can still call a static method or access some static data.
To set the data member or method to static, you only need to define the prefix and the keyword.
For example, the following code can generate a static data member and initialize it:

Class statictest {
Static int I = 47;
}

Now, although we have created two statictest objects, they only occupy one storage space of statictest. I. Both objects share the same I. Check the following code:
Statictest ST1 = new statictest ();
Statictest st2 = new statictest ();
At this time, both st1. I and st2. I have the same value of 47 because they reference the same memory area.
There are two ways to reference a static variable. As shown above, you can name it through an object, such as st2. I. You can also directly use its class name for reference, which is not feasible in non-static members (it is best to use this method to reference static variables, because it emphasizes the "static" nature of the variable ).
Statictest. I ++;
The ++ operator adds value to the variable. In this case, the values of st1. I and st2. I are 48.

Similar logic applies to static methods. A static method can be referenced through an object as in any other method, or by a special syntax format "class name. Method. Static methods are defined similarly:
Class staticfun {
Static void incr () {statictest. I ++ ;}
}
It can be seen that the staticfun method incr () makes static data I value-added. Call incr () using a typical method ():

Staticfun Sf = new staticfun ();
SF. incr ();

Alternatively, because incr () is a static method, you can directly call it through its class:
Staticfun. incr ();
For methods, static is an important purpose to help us call that method without having to create an object.
A simple example is as follows:
Public class teststatic {<br/> Public static void main (string ARGs []) {<br/> phonecard mycard_1 = new phonecard (); // create the first card object <br/> phonecard mycard_2 = new phonecard (); // create the second card object <br/> mycard_1.add0000= 0.8; // assign the additional charge to the first card. addamount is 0.8 <br/> // note that we did not assign a value to the second card. <br/> system. out. println ("first card surcharge:" + mycard_1.add.pdf); <br/> system. out. println ("additional fee for the second card:" + mycard_2.add.pdf); <br/> // No? In the output result, the additional fee for the second card is also 0.8. <Br/> system. out. println ("card surcharge:" + phonecard. addmedia ); <br/> // The printed output of this sentence indicates that the additional cost of the card class is 0.8 <br/>}< br/> class phonecard {<br/> static double addmedia; // static domain addmedia< br/>}< br/>
In this example, two classes are created. The phonecard class only defines one variable. Two phonecard class objects are created in the teststatic class, and a value is assigned to the additional charge addhandler for one of the objects, the other object is not assigned a value.

As shown in the preceding example, the static domain is stored in the public storage unit of the class, rather than in the storage unit of the object.

The same is true for static modification.

Ii. Final:
Final modifiable classes, fields (variables and constants), methods (without static modifier classes)

1. The final modifier class indicates that the class cannot be inherited.
For example, a final class is defined:
Final class snowbird {<br/> int I; <br/> string s; <br/> static void fly () {<br/> system. out. println ("Snowbird is flying"); <br/>}< br/> // define a class to inherit the snowbird class: <br/> public class bird extends snowbird {<br/> Public static void main (string [] ARGs) {<br/> snowbird. fly (); <br/>}< br/>
Copy the above two classes to the file and save the file name as bird. java. Now let's compile and see what will happen?
Error message: cannot inherit from final snowbird
Indicates that the final class cannot be inherited.

So what about final variable modification?
2. Final variable Modification
Various types of constants, such as 3.24268 and "201", are often defined in the program. At this time, we will use final to modify a name similar to a flag. For example:

Final string connectnumber = "201 ";
Final indicates that connectnumber is a constant and its value will not change throughout the process.

If final is removed, connectnumber becomes a variable.

Sometimes, to save space, constants are usually declared as static, because the static mentioned above uses the class memory space.

3. modification method:
The final modification method is called the final method. The final method cannot be redefined by the quilt class, that is, it cannot be overwritten.
If the parent class defines public void fly (){....}
Then the subclass cannot be defined.
Public void fly (){......}
But pay attention to the difference between overwriting and overloading. It cannot be overwritten and cannot be reloaded. You can also define
Public void fly (int I ){.....},

For example:
Class finalvalue {<br/> static final int I = 1; <br/> final void fly () {<br/> system. out. println ("Snowbird is flying over finalvalue"); <br/>}< br/> class testfinal extends finalvalue {<br/> int I = 2; <br/> void fly () {<br/> system. out. println ("Snowbird is flying over testfinal"); <br/> system. out. println ("in class finalvalue static final I =" + finalvalue. i); <br/> system. out. println ("in class testfinal I =" + I); <br/>}< br/> void fly (string s) {<br/> system. out. println ("Fly (" + S + ")"); <br/>}< br/> public class test {<br/> Public static void main (string ARGs []) {<br/> testfinal TF = new testfinal (); <br/> TF. fly (); <br/> TF. fly ("OK"); <br/> system. out. println (TF. i); <br/>}< br/>}
Save the above program as test. Java for compilation. What is the error?
Then, annotate void fly () {...} in the testfinal class.
That is
/* Void fly () {<br/> system. out. println ("Snowbird is flying over testfinal"); <br/> system. out. println ("in class finalvalue static final I =" + finalvalue. i); <br/> system. out. println ("in class testfinal I =" + I); <br/> }*/

Now let's see if the compilation is successful? We can see the difference between heavy load and coverage. I in finalvalue has nothing to do with I in testfinal. For variables, redefinition only hides the same-Name Domain of the parent class.

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.