In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what are the details of the use of Java code blocks". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the details of the use of Java code blocks".
1. Basic introduction
Code block, also known as initialization block, belongs to a member of a class (part of a class), similar to a method. Logical statements are encapsulated in the method body and held in {}
But unlike a method, there is no method name, no return, no parameters, only the method body, and does not have to be explicitly called through an object or class.
Basic grammar
(modifier) (optional) {Code}
Note:
1. The modifier is optional. To write, you can only write static.
two。 Code blocks can be divided into two categories, those decorated with static are called static code blocks, and those without static decoration are called normal code blocks.
3. Dispensable
Benefits
1. Equivalent to another form of constructor, can do initialization operation
two。 If there are duplicate statements in multiple constructors, they can be extracted into a block of code.
Quick start package com.demo.codeblock_;public class codeblock01 {public static void main (String [] args) {movie m01=new movie ("Pacific Rim"); movie m02=new movie ("the Wild Flying", 66); movie m03=new movie ("too busy to die", 55, "Old K");}} class movie {private String name; private double price; private String director {System.out.println ("Movie screen opens.") ; System.out.println ("the advertisement begins.") ; System.out.println ("the movie begins.") ;} / / three constructors overload public movie (String name) {/ / System.out.println ("Movie screen Open.") ; / / System.out.println ("Advertising begins.") ; / / System.out.println ("the movie begins.") ; System.out.println ("Constructor movie (String name) is called.") ; this.name = name;} public movie (String name, double price) {/ / System.out.println ("Movie screen Open.") ; / / System.out.println ("Advertising begins.") ; / / System.out.println ("the movie begins.") ; System.out.println ("Constructor movie (String name, double price) is called.") ; this.name = name; this.price = price;} public movie (String name, double price, String director) {/ / System.out.println ("Movie screen Open.") ; / / System.out.println ("Advertising begins.") ; / / System.out.println ("the movie begins.") ; System.out.println ("Constructor movie (String name, double price, String director) is called.") ; this.name = name; this.price = price; this.director = director;}}
two。 Code block details
Discussion on considerations and details in the use of code blocks
1) the static code block, also known as the static code block, is used to initialize the class, and it executes as the class loads, and only once. If it is a normal code block, every time an object is created, it is executed.
2) when the class is loaded [important!]
When ① creates an object instance (new)
② creates an instance of the subclass object, and the parent class is also loaded
When ③ uses static members of a class (static properties, static methods)
Case demonstration: static blocks of Class An extends Class B
3) ordinary code blocks are implicitly called when an object instance is created. Once created, it will be called once. If you only use static members of a class, the normal block of code will not execute.
Example of package com.demo.codeblock_;public class codeblock02 {public static void main (String [] args) {/ / class being loaded / / 1. New / / AA aa=new AA (); / / 2 when creating an object. Create an instance of the subclass object, and the parent class will be loaded, and the parent class will be loaded first, and then the subclass will be loaded AA aa01=new AA (); / / 3. System.out.println (cat.n) when using static members of a class; DD d1=new DD (); DD d2=new DD ();}} class DD {static {System.out.println ("static code of DD is executed once");}} class animal {static {System.out.println ("static code of animal is executed");}} class cat extends animal {public static int code 888 / / static code block static {System.out.println ("static code block of cat is executed");}} class BB {static {System.out.println ("static code of BB is executed");}} class AA extends BB {static {System.out.println ("static code of AA is executed");}}
The calling order of the class
When creating an object, call order in a class: (key, difficult)
① calls static code blocks and static attribute initialization (note: static code blocks and static attribute initialization calls have the same priority, and if there are multiple static code blocks and multiple static variables initialization, they are called in the order they define)
② calls the initialization of normal code blocks and ordinary attributes (Note: ordinary code blocks and universal attributes initialization have the same priority. If there are multiple ordinary code blocks and multiple unusual attributes initialization, they will be called in the order of definition)
③ calls the constructor.
Instance package com.demo.codeblock_;public class codeblock03 {public static void main (String [] args) {An a=new A ();}} class A {public A () {System.out.println ("No parameter construction of An is called");} int n2=getn2 (); {/ / normal code block System.out.println ("A's normal code block is called") } int getn2 () {System.out.println ("getn2 called"); return 99;} private static int n=getn (); static {System.out.println ("static code of An is called");} public static int getn () {System.out.println ("getn called"); return 100;}}
Code Block details 2
The front of the constructor actually implies super (and calling a normal code block, writing a new class to demonstrate a statically related code block, attribute initialization, and execution is finished when the class is loaded.
And therefore takes precedence over constructors and ordinary blocks of code
Class A {public AO {super0:// calls normal code block _ System.out.println ("ok");}} instance package com.demo.codeblock_;public class codeblock04 {public static void main (String [] args) {B b=new B ();}} class AA {{System.out.println ("normal code block of AA");} public AA () {/ / 1.super () / / 2. Call this class's normal code block System.out.println ("AA's constructor is called");}} class B extends AA {{System.out.println ("B's normal code block");} public B () {/ / 1.super () / / 2. Call the normal code block System.out.println of this class ("B's constructor is called");}}
Code Block details 2
When we create a subclass object (inheritance relationship), their static code block, static attribute initialization, normal code block, normal attribute initialization, and the order in which the constructors are called are as follows:
1. Static code blocks and static attributes of the parent class (hot lines in the order of definition, with the same priority
two。 Static code blocks and static attributes for subclasses (same priority, executed in defined order)
3. The normal code block of the parent class and the normal attribute initialization (same priority, in the order of definition)
4. The construction method of the parent class
5. Normal code blocks of subclasses and normal attribute initialization (same priority, executed in defined order)
6. The construction method of subclass
7. Static code blocks can only directly call static members (static attributes and static methods), while ordinary code blocks can call any member.
Instance package com.demo.codeblock_;public class codeblock05 {public static void main (String [] args) {/ / teacher's instructions / / (1) load the class / / 1.1 load the parent class A02 1.2 and then load B02 / / (2) create objects / / 2.1 start with the constructor of the subclass / / new B02 () / / object new C02 ();}} class A02 {/ / parent class private static int N1 = getVal01 (); static {System.out.println ("a static code block of A02."); / / (2)} {System.out.println ("the first normal code block of A02..."); / / (5)} public int n3 = getVal02 () / / initialization of normal attributes public static int getVal01 () {System.out.println ("getVal01"); / / (1) return 10;} public int getVal02 () {System.out.println ("getVal02"); / / (6) return 10 } public A02 () {/ / constructor / / hide / / super () / / initialization of normal code and common properties. System.out.println ("constructor of A02"); / / (7)} class C02 {private int N1 = 100; private static int N2 = 200; private void M1 () {} private static void m2 () {} static {/ / static code block, can only call static member / / System.out.println (N1); error System.out.println (N2) / / ok / / M1 (); / / error m2 ();} {/ / normal code block, you can use any member System.out.println (N1); System.out.println (N2); / / ok M1 (); m2 ();}} class B02 extends A02 {/ / private static int n3 = getVal03 () Static {System.out.println ("a static code block of B02..."); / / (4)} public int N5 = getVal04 (); {System.out.println ("the first common code block of B02..."); / / (9)} public static int getVal03 () {System.out.println ("getVal03"); / / (3) return 10 } public int getVal04 () {System.out.println ("getVal04"); / / (8) return 10; / / be sure to remove the product slowly. Public B02 () {/ / constructor / / hides / / super () / / initialization of normal code blocks and common attributes. System.out.println ("constructor of B02"); / / (10) / / TODO Auto-generated constructor stub}}
Thank you for your reading, the above is "what are the details of the use of Java code blocks?" after the study of this article, I believe you have a deeper understanding of the details of the use of Java code blocks, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.