In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "static keywords and static variables and static methods in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
I. static keyword
Variables and methods modified by static are called class variables (or static variables) and class methods (or static methods) respectively, while variables and methods that are not modified by static are called instance variables and instance methods, respectively.
Static variables
1. What is a static variable
Modify the member variable with the static keyword, which is called a static variable.
two。 For example, if there are multiple circular objects, each circular object has its own radius (radius) and pi (PI), because pi is a fixed constant 3.141596 and does not have to be owned by each circular object. Pi (PI) as shared, the radius of each circular object may be different.
3. How to use static variables
For example:
/ / Circular class public class Ball {Radius of public double radius;// circle static double PI=3.141956;// pi public static void main (String [] args) {/ / TODO Auto-generated method stub Ball b1=new Ball (); / / create the first circular object Ball b2=new Ball (); / / create the second circular object Ball.PI=3.141596;// static variable assignment b1.radiuscircle 5ramp / assign b2.radius=8 to the radius of the first circle / / assign System.out.println to the radius of the first circle ("pi of the first circle:" + b1.Pi + "" + "radius is:" + b1.radius); / / print the pi and radius System.out.println of the first circle ("the pi of the second circular object:" + b2.Pi + "" + "radius is:" + b2.radius); / / print the pi and radius of the second circle}
The result of the output is:
The first circular object has a pi of 3.141956 and a radius of 5.0.
The pi of the second circular object: 3.141956 radius: 8.0
In the above code, a static variable PI is defined in the Ball class to represent pi, which is shared by all instances, because this PI is a static variable that can be called directly using Ball.PI, or instantiated object call, b1.PI. The first circular object and the second circular object both have a PI property of 3.141596.
Third, static method
1. What is a static method
Static variables and methods can be accessed without creating an object. Static methods are called static methods by adding static to the definition methods of the class.
two。 The access format of static methods is the same as static variables
Class name. Method name
3. How to use static methods
Public class Ball01 {radius of public double radius;// circle public static double PI=3.141596;// pi / / angular rotation public static double toRadius (double angdeg) {return angdeg* (Ball01.PI/180);} public static void main (String [] args) {/ / TODO Auto-generated method stub / / Ball01 b1=new Ball01 (); / / create an object System.out.print (Ball01.toRadius) of a circle; / / call the static method of the object}}
The result of the output is:
1.745331111111111
In the above code, a static method toRadius () angle rotation is defined in the Ball01 class, which invokes the static method of the object through object instantiation. A static method can only access static-decorated members, because members that are not decorated by static need to create objects before they can be accessed, while static method calls do not have to create objects.
Static code block
1. What is a static code block
A static code block is code that surrounds many lines of code with curly braces {} and is decorated with the static keyword.
two。 Executes as the class loads, and only once. Static code blocks are typically used to initialize class member variables.
3. How to write static and non-static code blocks
Static code block:
Static code block: static {} non-static code block: {}
4. How to use static code blocks
For example:
Class Person3 {static String name; / / static code block static {name= "Li Hua"; System.out.println ("static code execution of Person3 class.");}} public class test {/ / static code block static {System.out.println ("static code execution of test class.");} public static void main (String [] args) {/ / TODO Auto-generated method stub Person3 p1=new Person3 () / / create the first Person3 object Person3 p2=new Person3 (); / / create the second Person3 object}}
The result of the operation is:
Test the static code execution of the class.
Static code execution for the Person3 class.
We can see from the results of the run that both static blocks of code are executed. The first step is to load the class test, load the static code block while loading the class, and then execute the main program main (). Create two Person3 objects in the main method, and after instantiating the object twice, the static code executes only once, indicating that it executes as the class loads, and only once.
This is the end of the introduction of "static keywords and static variables, static methods in Java". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.