Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the execution order of java static code blocks, construction code blocks, and constructors

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not quite understand the knowledge points of this article "what is the execution order of java static code blocks, construction code blocks and construction methods", so the editor summarizes the following contents for you. The content is detailed, the steps are clear, and it has a certain reference value. I hope you can gain something after reading this article. Let's take a look at this article entitled "what is the order in which java static code blocks, construction code blocks, and constructors are executed?"

The execution order is: first execute the "static code block", then execute the "construction code block", and finally execute the "construction code block". The static code block is class-level, while the construction code block and the construction method are instance-level, so the static code block is executed first; and because the construction code block is independent, it must be attached to the carrier to run, so the construction code block needs to be placed in front of the constructor.

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

Construction code blocks, static code blocks, and construction methods are three different code blocks in a class, so what's the difference between them?

Introduction to static code blocks: declare with staitc, execute when the class is loaded by jvm, and execute only once

Construction code block: defined directly in the class with {}, executed each time an object is created.

Execution order priority: static blocks, main (), building blocks, construction methods.

Class A {/ / Construction code block {System.out.println ("Construction code block A");} / / static code block static {System.out.println ("static code block A");} / / Construction method public A () {System.out.println ("constructor A");}} execution sequence of two and three

In order to find out the execution order of the three, we instantiate class An and test single instance and multiple instances respectively.

2.1 single instance class Demo {public static void main (String [] args) {new A ();}}

2.2 multiple instances class Demo {public static void main (String [] args) {new A ();}}

3. Sequential class B extends A {/ / construct code block {System.out.println ("construction code block B");} / / static code block static {System.out.println ("static code block B");} / / construction method public B () {System.out.println ("construction method B") }} class Demo {public static void main (String [] args) {new B ();}}

The above is about "java static code block, construction code block, construction method execution order is how" this article, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge content, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report