The difference between static and final in Java

Source: Internet
Author: User

The final defined variable can be regarded as a constant and cannot be changed;
The final defined method cannot be overwritten;
A final defined class cannot be inherited.
Final static is just a static feature.

Static and final are not directly related.

Static is the allocation of an area in memory for the entire class to be common, and all classes of objects enjoy its common values

The difference between static and final
First, Static:
Under what circumstances should we use static?
1, just want to use a storage area to save a specific data-no matter how many objects to create, or even do not create the object at all.
2. We need a special method, which is not associated with any object of this class. That is, even if you do not create an object, you need a method that can be called.
To meet these two requirements, you can use the static (static) keyword.
Let me give you an example:

Once something is set to static, the data or method will not be associated with any object instances of that class. So although an object of that class has never been created, you can still invoke a static method, or access some static data.
In order to set the data member or method to static, you only need to define the predecessor and this 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 made two Statictest objects, they still occupy only one storage space of statictest.i. Both of these objects share the same i. Please examine the following code:
Statictest st1 = new Statictest ();
Statictest st2 = new Statictest ();
At this point, both st1.i and st2.i have the same value of 47, because they refer to the same memory area.
There are two ways to reference a static variable. As shown above, it can be named by an object, such as st2.i. It can also be referenced directly by its class name, which is not feasible in non-static members (it is best to refer to the static variable as it emphasizes the "static" nature of the variable).
statictest.i++;
where the + + operator adds value to the variable. At this point, the value of either st1.i or ST2.I is 48.

Similar logic applies to static methods as well. You can refer to a static method as you would any other method by using an object, or a special syntax format "class name. Method ()". The definition of a static method is similar:
Class Staticfun {
static void Incr () {statictest.i++;}
}
It can be seen from this that the Staticfun method incr () adds value to the static data I. A typical method call INCR () can be used:

Staticfun SF = new Staticfun ();
SF.INCR ();

Or, because INCR () is a static method, it can be called directly from its class:
STATICFUN.INCR ();
For a method, static is an important use to help us invoke that method without having to create an object.
A simple example is as follows:
public class Teststatic {
public static void Main (String args[]) {
Phonecard mycard_1 = new Phonecard ();//Create First card object
Phonecard mycard_2 = new Phonecard ();//Create Second card object

Mycard_1.addfee = 0.8;//to the first card with an additional charge of Addfee assigned to a value of 0.8
Notice that we didn't assign a second card.

System.out.println ("The first card's surcharge:" + Mycard_1.addfee);
System.out.println ("Additional charge for the second card:" + Mycard_2.addfee);
Did you find it? The additional charge for the second card in the output is also 0.8.
SYSTEM.OUT.PRINTLN ("card surcharges:" + phonecard.addfee);
The printout of the sentence indicates that the card class has a 0.8 surcharge.
}
}

Class phonecard{
Static Double addfee;//Addfee
}

This example creates two classes, the Phonecard class defines only one variable, two Phonecard class objects are created in the Teststatic class, and a surcharge addfee assigned to one of the objects, while the other object is not assigned a value.

As can be seen from the above example, the static domain holds the common storage unit of the class, not the storage unit of the object.

The static modification method is the same.


Second, Final:
Final can modify classes, fields (variables and constants), methods (and static does not decorate classes)

1. Final modifier class, indicating that the class cannot be inherited.
If a final class is defined:
Final class snowbird{
int i;
String s;
static void Fly () {
System.out.println ("Snowbird is flying");
}
}

Now define a class that attempts to inherit the Snowbird class:

public class Bird extends snowbird{
public static void Main (string[] args) {
Snowbird.fly ();
}
}

Copy the above two classes to a file, save the file name as Bird.java, and now compile to see what the problem is?
The error message is: cannot inherit from final Snowbird
Indicates that the final class cannot be inherited.

So what about the final modified variable?
2. Final modified variable
It is often necessary to define constants of various types, such as: 3.24268, "201" and so on. At this point we will use final to decorate a name that resembles a marker. Such as:

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

If final is removed, the connectnumber becomes a variable.

Sometimes in order to save space, a constant is usually declared static. Because the static as described above uses the memory space of the class.

3. Modification Method:
The final modified method, called the ultimate method. The final method cannot be redefined by the quilt class, i.e. it cannot be overwritten.
If the parent class defines public void Fly () {...}
The subclass cannot be defined
public void Fly () {... }
But notice the difference between overriding and overloading. Cannot be overridden not cannot be overloaded, as you can also define
public void Fly (int i) {...},

Here's an example:

Class Finalvalue {
static final int i = 1;
Final void Fly () {
System.out.println ("Snowbird is flying over Finalvalue");
}
}

Class Testfinal extends Finalvalue {
int i = 2;
void Fly () {
System.out.println ("Snowbird is flying over testfinal");
System.out.println ("in class Finalvalue static Final i =" + finalvalue.i);
System.out.println ("in class testfinal i =" + i);
}
void Fly (String s) {
System.out.println ("Fly (" + S + ")");
}
}

public class Test {
public static void Main (String args[]) {
testfinal tf = new testfinal ();
Tf.fly ();
Tf.fly ("OK");
System.out.println (TF.I);
}
}
Save the above program as Test.java compile to see what error occurred?
Then, comment out the void fly () {...} in the Testfinal class.
is as follows
/* void Fly () {
System.out.println ("Snowbird is flying over testfinal");
System.out.println ("in class Finalvalue static Final i =" + finalvalue.i);
System.out.println ("in class testfinal i =" + i);
}*/

Do you want to compile now and see if it's passed? The difference between the visible overload and the overlay. It is also found that I in Finalvalue and I in testfinal do not matter. Because for a variable, a redefinition is just a domain with the same name as the parent class being hidden. This article is from 51CTO. COM Technology Blog

The difference between static and final in Java

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.