Java's static and final use traps

Source: Internet
Author: User

    1. For a final type of member variable, there are generally two ways of assigning an initial value

      1. Assign an initial value when declaring a member variable of the final type

      2. The initial value is not assigned when declaring a member variable of the final type, but it is assigned an initial value in all the struct methods of the class

    2. Static block of code: code block

Package Com.yuanzijain08;class p{static {System.out.println ("static Block");} Public P () {System.out.println ("P Constatictor");}} public class Statictest {public static void main (string[] args) {//TODO auto-generated method stub p p = new P ();}}

Execution results

Static BLOCKP Constatictor


3.

Package Com.yuanzijain08;class p{static {System.out.println ("P static Block");} Public P () {System.out.println ("P contructor");}} Class Q extends P{static {System.out.println ("Q static Block");} Public Q () {System.out.println ("Q Contructor");}} Class S extends q{static {System.out.println ("s static Block");} Public S () {System.out.println ("s Contructor");}} public class Statictest {public static void main (string[] args) {//TODO auto-generated method stub new S ();}}

Execution results

P static blockq static BlockS static BLOCKP Contructorq contructors contructor

4. Static code block. The function of static code blocks is also to do some initialization work, first execute static code block, and then execute the construction method, static code block when the class is loaded, and the construction method is executed when the object is generated, in order to invoke a class to build the object, first to load the class into the Java virtual machine, The JVM then loads the class to generate the object

Package Com.yuanzijain08;class p{static {System.out.println ("P static Block");} Public P () {System.out.println ("P contructor");}} Class Q extends P{static {System.out.println ("Q static Block");} Public Q () {System.out.println ("Q Contructor");}} Class S extends q{static {System.out.println ("s static Block");} Public S () {System.out.println ("s Contructor");}} public class Statictest {public static void main (string[] args) {//TODO auto-generated method stub new S (); new S ();}}

Execution results

P static blockq static BlockS static BLOCKP Contructorq contructors Contructorp Contructorq contructors contructor

5. The static block of code for a class executes only once, when the class is loaded, because each class is only loaded once, so the static code block is only executed faintly, and the constructor is not constructed, and the constructor is called every time an object is generated, so the new one will call the constructor method

6. If the inheritance system has both a construction method and a static code block, then first go to the top-level class of code block, and then execute to the lowest static code block, and then go to the top-level class construction method, the most basic construction method has been performed, note: Static code block can only be executed once

7. Non-static member variables cannot be accessed in static methods, static member variables can be accessed in static methods, static member variables can be accessed in non-static methods

Summary: Static can only access static, non-static access to all methods

8. The This keyword can no longer be used in a static method

Java's static and final use traps

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.