For java initialization problems, remember three points ~~~, Java Initialization

Source: Internet
Author: User

For java initialization problems, remember three points ~~~, Java Initialization

I saw the post discussion and continued to discuss java initialization. Then I debug it, which is similar to what I thought. Then write it out and share it with everyone. Remember the three points and you will not be wrong ~~~ Pitfalls ~~


Package com. lean; public class InitJava {/** static variable initialization, constructor call (execute each time, initialize non-static variables, methods, to prevent constructor from calling non-static method attributes) * constructor execution, main function execution ** in fact, remember three points ~~~~ I am so depressed that I will summarize it to you: * 1. load the static variable sequentially * 2. no matter when the constructor is executed, the non-static variables and methods must be loaded before the constructor method body is executed (no limit) * 3. after static execution is complete, classloader executes the main method body and then runs normally ~~~ */Public static int k = 0; public static InitJava t1 = new InitJava ("t1"); public static InitJava t2 = new InitJava ("t2 "); public static int I = print ("I"); // public static int n = 99; private int a = 0; public int j = print ("j "); {print ("construct block");} static {print ("static block");} public InitJava (String str) {System. out. println (++ k) + ":" + str + "I =" + I + "n =" + n); ++ I; ++ n ;} public static int print (String str) {System. out. println (++ k) + ":" + str + "I =" + I + "n =" + n); ++ n; return ++ I ;} public static void main (String args []) {InitJava t = new InitJava ("init ");}}

1: j I = 0 n = 02: constructed block I = 1 n = 13: t1 I = 2 n = 24: j I = 3 n = 35: construct block I = 4 n = 46: t2 I = 5 n = 57: I = 6 n = 68: static block I = 7 n = 999: j I = 8 n = 10010: constructed block I = 9 n = 10111: init I = 10 n = 102

Isn't it easy ~~



Java initialization error

Obviously there is a problem with the profile struts-default.xml.
Either the struts-default.xml cannot be found, or the struts-default.xml file is not written to the correct.

Java variable initialization Problems

Java is A rigorous syntax language. In fact, if FileNotFoundException occurs in the new FileOutputStream ("C:/PS1.txt") in your A sentence, the value assignment statement of the previous fos cannot be executed. Therefore, the compiler prompts you that no Initialization is required. In fact, to remove this compilation error, you only need to change FileOutputStream fos to FileOutputStream fos = null.

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.