Java static variable initialization

Source: Internet
Author: User

Java static variable initialization
Static final int NUMBER_OF_MONTHS = 12;
Static final float PI = (float) 22/7;
If you want a static variable to be referenced elsewhere

Public static final int NUMBER_OF_MONTHS = 12;
Public static final float PI = (float) 22/7;

Written in the function

Public class MainClass {

Public static void main (String [] arg ){
Final int FEET_PER_YARD = 3; // Constant values
Final double MM_PER_INCH = 25.4; // that cannot be changed

System. out. println (FEET_PER_YARD );
System. out. println (MM_PER_INCH );
   
  }

}


Static Variable initialization
A static block can be defined in the Java class for static variable initialization. For example:

 

Public class Test {
Public static int _ I;
Static {
_ I = 10;
     }   
}  
Public class Test {public static int _ I; static {_ I = 10 ;}}

Of course, the most common operation to initialize static variables is to directly assign values when declaring variables. For example:

 

Public class Test {
Public static int _ I = 10;
}  
Public class Test {public static int _ I = 10 ;}

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.