Java keyword final and static

Source: Internet
Author: User

First, Final keywords

The 1.final class cannot be inherited, so there is no subclass. Examples are as follows: will be error 650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M00/57/21/wKiom1SSP_ Gydoswaacik4f7lcu820.jpg "title=" final class cannot be inherited. jpg "alt=" wkiom1ssp_gydoswaacik4f7lcu820.jpg "/>

2.final method by the way review the overloaded and rewritten

2.1 Overloading Examples:

public class Testfinalclass {

public static void Main (String args[]) {

}

public void Add (int i) {

System.out.print ("parents. Add ... ");

}

}

Class Testchildrenclass extends testfinalclass{

public void Add (int i,int j) {

System.out.print ("Children...add");

System.out.print ("33333");

}

}

2.2 Overriding Override

public class Testfinalclass {

private String name;

public static void Main (String args[]) {

New Testchildrenclass (). Add (0);

}

public void Add (int i) {

System.out.print ("parents. Add ... ");

}

}

Class Testchildrenclass extends testfinalclass{

public void Add (int i) {

System.out.print ("Children...add");

System.out.print ("33333");

}

}

2.3 If the parent does not want the subclass to override one of its methods, the method can be defined with final, as in the following example

public class Testfinalclass {

private String name;

public static void Main (String args[]) {

New Testchildrenclass (). Add (0);

}

Public final void Add (int i) {

System.out.print ("parents. Add ... ");

}

}

Class Testchildrenclass extends testfinalclass{

Public void Add (int i) {//This will be an error, cannot be rewritten

System.out.print ("Children...add");

System.out.print ("33333");

}

}

In addition to the use of the above, there is also the use of efficiency. The compiler goes into an inline mechanism when it encounters a call to the final method, greatly improving execution efficiency.

3.final variables

public class Testfinalclass {

Public final static String s = "... final S";

public final int E = 1; Final member variable must pay initial value

private String name;

public static void Main (String args[]) {


}

public static void Add (int i) {

Final String m; Final non-member variables can be used without initial values

m = "... m"; Given initial value

M = ". SDJF ";//error cannot give value again

}

}

4.final parameters

public class Testfinalclass {

public static void Main (String args[]) {

}

public static void Add (final int i) {

System.out.print ("+i"); Only use this parameter

i = 0;//cannot give value

}

}

Second, the Static keyword

1.static method

public class Testfinalclass {

public static void Main (String args[]) {

Testchildrenclass.add (0); You can reference this static method before the class is created

}

}

Class Testchildrenclass {

public static void Add (int i) {//However, it cannot be referenced by an instance of the class.

System.out.print ("Children...add");

}

}

2.static member variables

public class Testfinalclass {

public static void Main (String args[]) {

String name = testchildrenclass.name;//static Member

}

}

Class Testchildrenclass {

public static String name = "user";// cannot be referenced by an instance of the class, and cannot be used when modified with private

}

3. When static is used in conjunction with final to modify member variables and member methods, it can be simply understood as "global constants"!
3.1 For variables, this means that once a value is given it cannot be modified and is accessible through the class name.
3.2 For methods, the representation is not overwritten and can be accessed directly through the class name.














This article is from the "Small blog" blog, be sure to keep this source http://9686567.blog.51cto.com/9676567/1591466

Java keyword final and static

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.