Java static member variable, static code block execution test

Source: Internet
Author: User

Java class object static member variables, static code blocks load execution order.

Package com.yjm.pro;import java.io.ioexception;import java.util.properties;public class  Pro { public static String url; public static String  username; public static string password; public static string  classforname; public static string log; public static properties  PROPERTIES; STATIC {  SYSTEM.OUT.PRINTLN ("Class static code block    Execute test ....");   Properties = new properties ();   try {   properties.load ( Pro.class.getResourceAsStream ("pro.properties"));    url = properties.getproperty ("url ");    username = properties.getproperty (" username ");    password =  properties.getproperty ("password");    log = properties.getproperty ("Log");    classforname = propertiEs     .getproperty ("Classforname");  } catch  (IOException e )  {   e.printstacktrace ();   } }}

Test code

public static void Main (string[] args) {//test1 ();  Test2 ();  Test3 (); Test4 ();  } private static void Test4 () {System.out.println (pro.class); SYSTEM.OUT.PRINTLN ("Class static variable" + Pro.log); }

Execution results

Class Com.yjm.pro.Pro
Class static code block executes test ....
Class static Variable Yes

Test class static data that executes static code blocks only when the class is referenced.

Java static member variable, static code block execution test

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.