Java static,final Basic Concepts

Source: Internet
Author: User

(reprinted from Http://blog.kenyang.net/2011/03/java-staticfinal.html) I've learned a long time ago that

Not only to know know how,

And know know why,

For example, when I send work to the people below,

For example, to make a feature that sends mail,

Well, with the gift of Google God, it's usually quick to find code that sends mail.

But when I go through every line of code,

Mostly ..... Don't know. "

I feel this wind is not long.

Even some of the students don't know when to use static and final.

Also do not know what is interface, abstract and so on.

Because they don't know what these things do.

So use this article to explain a little bit.

Static:

The meaning of meditation

Can be used to declare a function or a change,

Just remember that when a function or a change is declared static,

He has the concept of a unique value!!

He would occupy the memory space forever.

The value will always be the same regardless of the new object.

For example!

We see the following code example

Class test{     static int ivalue = 0;     Public test () {     }}public class Main {public     static void Main (string[] argv)  {          Test test1 = new test (); 
   test test2 = new test ();          System.out.println (test1.ivalue);          Test2.ivalue  = ten;          System.out.println (test2.ivalue);          System.out.println (Test1.ivalue);     } }

Let's declare the information in test this category ivalue as static,

And then, in Main, divide the new two objects out.

It's test1, Test2,

Then print out the Test1 ivalue, (print out 0, of course!). Start is 0)

Then we use test2 this object to set the Ivalue value to 10,

Then go and print out the Test2 ivalue, (print out 10, of course!) Because it's 10.)

It's a bit heavy!

At this time, print out the Test1 ivalue!

Most people will feel that it should be printed out 0 ah,

But the print is 10.

The reason is because we declared the Ivalue static.

And Ivalue became a common variable.

This is a feature of static!

feature one: Forever will be a unique value!!


And Static has a feature,

feature two: static can be accessed through the type of direct access to use!

It means that we do not need a new object to come out.

You can also access the variables directly through the category.

Examples are as follows!

Class test{     static int ivalue = 0;     Public test () {     }}public class Main {public     static void Main (string[] argv)  {          System.out.println ( Test.ivalue);}     }

You'll find in main that we don't have a new object coming out!!

You can also access the information in the test category (Ivalue)

The static introduction is probably here,

Then we introduce final

Final

The concept of static,final is much easier to understand!

Can be used to declare a category, function, or variable.

Category: When declared in the category, the class can not be inherited!

Function: When a function is declared final, the sub-category of his is not to be covered.

Variables: When a variable is declared final, it means that he is a constant and cannot be modified.


Attention!

Using static final requires a little attention!!

As mentioned above, when we have a constant to declare,

will announce the following

public static final String strvalue = "Ken";

Why do you declare that there is a risk?

Because Java is compile in the line,

will be declared as static final, directly into the program.

What does that mean?

System.out.println (strvalue);

When you have code that's like accessing strvalue,

We're going to turn Java into compile class,decompile.

Will discover that it becomes the following code

System.out.println ("Ken");


Okay, well, where the hell is the risk?

Of course, if you could use some good ides today,

Should not encounter such risks,

In eclipse, for example, if you save or execute, Eclipse will help you with the compile!.
(If there is a tick to build automatically)

But if you are the Apostle Alchemy today!

When I compile the order ...

You changed the program and changed the value of strvalue to "Yang".

Well, you forgot to do compile ....

At this time ... If someone accesses strvalue, it will make a mistake!!

Then how to solve???

We can use the static block to make the declared motion!!!

As follows:

Static String strvalue;static{     strvalue = "Ken";}

If you use this way of declaring, you can avoid the above mentioned problem!


Forgive me, too tired.

Let's start with the basic concepts of static,final today.

The next chapter is interface, abstract!!

Java static,final Basic Concepts

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.