In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
How to understand the Java code block, in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
Static code block
First of all, let's review the static keyword. The static keyword modifies the class and its members as well as the constructor, which is characterized by resources that are loaded before the class creates an object and are shared by all objects. For example, static methods can be called directly by the class name, without the need to use the new keyword to create the object. Comparative static methods for static and non-static methods:
Static methods can only call static members, not non-static members.
No this object
Non-static method:
Non-static methods can call either non-static or static members
Static code block scope: outside of the inner methods of the class, decorate with static
Public class CodeBlock {static {System.out.println ("this is a static code block");}}
When to execute: static code blocks run when the class is loaded, and only once, and take precedence over various code blocks and constructors. If there are multiple static blocks of code in a class, they will be executed in writing order
Questions about static code blocks:
Can static code blocks access methods? This question needs to be answered from the static method above. Static code blocks can call static methods, but cannot call non-static methods.
Can static code blocks be placed in the method body? This is not allowed, even static methods can not put static code blocks, and static code blocks can only be placed inside the class, can not be placed inside static methods, because static blocks are executed when the class is loaded, and static methods need to be called before execution, there is a conflict here.
Local code block
Scope: exists in the method
Public static void main (String [] args) {{int number = 1;} System.out.println (number); / / exception}
The execution order of the local code block is the same as that of ordinary statements, except that the variables in the code block cannot be called outside the code block. As shown above, the output of number is abnormal, which effectively controls the life cycle of the variables, and releases as early as possible to improve memory usage.
Construct code block
Scope of the construction code block: inside and outside the method, the difference with the static code block is that there is no static decoration, and the public class CodeBlock {{System.out.println ("this is the construction code block") is executed when the class creates an object;}} the construction code block is executed only when the object of the class is created, each time an instance of the class is created, if there are multiple construction code blocks, the execution order is from top to bottom. Homologous code block
Synchronous code block scope: code blocks decorated with synchronized inside the method
Public class CodeBlock {public void syncMethod (Object obj) {synchronized (obj) {
}}}
Synchronized is essentially a lock, so why do you need a lock? Resource preemption occurs when multiple threads access shared data at the same time. If it is not processed, it is possible that the data obtained by each thread is incorrect. Here we can use synchronization method or synchronous code block. When a thread comes in, we need to keep out other threads and not let other threads use the resource until the thread has finished using it.
Code block execution order public class CodeBlock {static {System.out.println ("static code block");} {System.out.println ("construct code block");} public CodeBlock () {System.out.println ("no parameter constructor");}
Public void sayHello () {{System.out.println ("local code block");}}
Public static void main (String [] args) {System.out.println ("main method executed")
New CodeBlock () .sayHello () } the execution result static code block executes the main method constructor code block no parameter constructor local code block execution order is: static code block-> construction code block-> constructor-- > local code block (at the same level as synchronous code block) about the code block execution order of the parent and child classes, it does not take much time here, the execution order of the parent class is executed before the subclass Unless you are overriding the method of the parent class. This is the answer to the question on how to understand the Java code block. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.