In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to use the static keyword in Java to achieve static initialization blocks, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
In a class, member variables declared with static are static member variables, also known as class variables. The life cycle of a class variable is the same as that of a class and is valid throughout the execution of the application. It has the following characteristics:
1. Is a common variable of the class, belongs to the class, is shared by all instances of the class, and is explicitly initialized when the class is loaded.
two。 For all objects of this class, there is only one static member variable. Shared by all objects of this class!
3. It is generally called with "class name. class properties / methods. (static members can also be accessed through object references or class names that do not need to be instantiated.)
4. Non-static members cannot be accessed directly in the static method.
Core points:
Static-decorated member variables and methods, subordinate to the class.
Ordinary variables and methods belong to the object.
[example 4-10] use of the static keyword
/ * Test the use of the static keyword * @ author Gao Qi * * / public class User2 {int id; / / id String name; / / account name String pwd; / / password static String company = "Beijing Shangxue School"; / / Company name public User2 (int id, String name) {this.id = id; this.name = name } public void login () {printCompany (); System.out.println (company); System.out.println ("login:" + name);} public static void printCompany () {/ / login (); / / call non-static member, compilation will report System.out.println (company) } public static void main (String [] args) {User2 u = new User2 (101Gao Xiao Qi); User2.printCompany (); User2.company = "Grandpa Ali in Beijing"; User2.printCompany ();}}
Static initialization block
Constructor is used for object initialization! Static initialization block, used for class initialization operation! Non-static members cannot be accessed directly in a static initialization block.
Note:
Static initialization block execution order (see here after learning inheritance):
1. Back to the Object class, first execute the static initialization block of Object, and then execute the static initialization block of the subclass down to the static initialization block of our class.
two。 The constructor executes in the same order as above!
The following static initialization blocks are executed with the class loading
Public class User3 {int id; / / id String name; / / account name String pwd; / / password static String company; / / Company name static {System.out.println ("initialization of the execution class"); company = "Beijing Shangxue School"; printCompany ();} public static void printCompany () {System.out.println (company) } public static void main (String [] args) {User3 U3 = new User3 ();}
The above is how to use the static keyword to achieve static initialization blocks in Java. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow 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.
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.