Java Basics (2)--use of the keyword final

Source: Internet
Author: User

/**
Use of *final Keywords
* @author lihaiming
*email:[email protected]
* Keyword final can be decorated with properties, methods, classes
* When modifying a property, the final modified variable is a constant
* When modifying a method, the method is not overridden by the quilt class
* Class is not inherited when class is modified
*/

public class testfinal{
When the final modifier variable is constant, it cannot be assigned again;
private final int i=100;
Static is a global variable
private static int j=50;

private final int ID;
Final modified reference type; non-modifiable space;
Final Li L = new Li ();

Use the final, static keyword to modify the variable, the variable is a global constant, can only invoke the use, can not assign value and other operations;
Public testfinal () {
Id=++j;
}
public static void Main (string[] args) {
testfinal tf = new testfinal ();
SYSTEM.OUT.PRINTLN (TF. I);
System.out.println (J);

System.out.println ("------------------");
testfinal TF1 = new Testfinal ();
Testfinal TF2 = new Testfinal ();
System.out.println (tf1.id);
System.out.println (tf2.id);


System.out.println ("------------------");
System.out.println (Tf.l.geti ());
Tf.l.seti (56);
System.out.println (Tf.l.geti ());
}
}

Class li{
public int i=10;
public int j=12;

Public Li () {}

public void SetI (int i) {
This.i=i;
}
public int Geti () {
return i;
}
}

Program results:

100
51
------------------
52
53
------------------
10
56

Java Basics (2)--use of the keyword final

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.