JAVA static code block and java static code

Source: Internet
Author: User

JAVA static code block and java static code

Feature: it is executed with the class loading, and will only be executed once, and it takes precedence over the main function.
Used to initialize a class.

/* Static code block format: static {execution statement in the static code block} features: it is executed as the class is loaded and only once, and takes precedence over the main function. Is used to initialize the class */class Person {static {System. out. println ("I Am a static code block");} {System. out. println ("I am constructing a code block");} public static void show () {System. out. println ("hello") ;}} public class Test {public static void main (String [] args) {Person. show (); // call the static method Person. show (); // call the static method new Person (); // extended knowledge: anonymous new object }}

 

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.