------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you!
Object-oriented-knowledge about static (static)
You can use the static keyword to declare variables, methods, or blocks of code that are not related to the class, but not to the instance.
Static variables:
When you write a class, you are actually describing the behavior and properties of its objects, and only by using the new keyword will the system allocate memory space to the object for the object to be created.
However, sometimes users may not want to have a single value for certain data regardless of whether they have objects, which can be accessed directly by the class rather than by the object.
Such specific data is a static variable. A variable modified with static is called a static variable, also called a static member or a property called a class.
Static variables have the following considerations:
1, static variables in a class belong to a class, not to a particular object.
2, a static member of a class can be used with the name of a class without having to create an object of the class.
3, a static variable or method is also known as a variable or method of a class.
4, no matter how many instances of a class are created, there is only one copy of the static variable in the entire class.
5, it is recommended to invoke a class name when referencing a static variable.
6, a variable declared as static is essentially a global variable
1 classChinese {2 3 StaticString country = "China";4 5 String name;6 7 intAge ;8 9 Ten One voidSingourcuntry () { A -System.out.println ("My Dear" +country); - the } - - } - + - + Public classTestchinese { A at Public Static voidMain (string[] args) { - - //to access static variables by class name - - System.out.println (chinese.country); - in - toChinese CH1 =NewChinese (); + - //using objects to access static variables is not recommended the * System.out.println (ch1.country); $ Panax Notoginseng ch1.singourcuntry (); - the } + A } the +
static method:
Sometimes users also want to create methods that can be called without objects, such as themain method.
A static method, like the main method, that is modified by static. The purpose is to make the method independent of the class instance, using the class to access it, rather than using an instance of the class, so it is also called a class method.
Static methods have the following characteristics:
1, a static method of a class can only access other static members and cannot access non-static members.
2, there is no this keyword in the static method .
3, static methods cannot be overwritten (overridden) as non-static methods.
1 classStaticfun {2 3 Static inti = 10;//Static Variables4 5 intJ;6 7 8 9 Static voidSetValue (intx) {//Static MethodsTen Onej = x;//compilation Error A -System.out.println ("" +i); - the } - -}
Description: Static methods cannot access any other variables except local variables, static properties, and their parameters. If you attempt to access a non-static property, it causes a compilation error.
Static code block:
The so-called static code block, is a code fragment composed of static and {} ,
The syntax is:
1 Static {23 // Static code block 4 5 }
Static code blocks are used with the following considerations:
1, if you need to initialize a static variable through a computer, you can declare a static block
2, a static block executes only once when the class is loaded.
3, only static data members can be initialized.
4, if the class contains more than one static block, it is executed separately in the order in which it appears in the class.
Example: By defining a static block of code to randomly generate A number of elements to assign a value
1 classtryinitialization {2 3 Static int[] values =New int[10];4 5 Static{//Static statement block6 7 for(inti = 0;i<values.length;i++){8 9Values[i] = (int) (100.0*math.random ());Ten One } A - } - the } - -
Static statements can initialize static variables, and constructor methods are used to initialize member variables, and whose precedence is high when creating objects?
1 Public classteststatic {2 3 Static{4 5System.out.println ("I am the statement block output");6 7 }8 9 Publicteststatic () {Ten OneSystem.out.println ("I Am a Builder"); A - } - the Public Static voidMain (string[] args) { - -System.out.println ("constructor called before"); - +Teststatic TC =Newteststatic (); - +System.out.println ("after constructor call")); A at } - -}
Output Result:
I'm a statement block output
Before the constructor is called
I'm the constructor.
After the constructor is called
Static import:
If you access a static member of a class, you must qualify a reference to the class.
Since Java SE5.0 ,theJava language provides a static import feature that allows users to have unqualified access to static members without the use of qualified class names.
math
1 import Java.lang.Math; 3 class Caldemo { 4 5 public void Say () { 6 7 System.out.println ("sqrt" +math.sqrt (5.18 9 } 10 11 }
Use the static import notation:
1 import static Java.lang.Math;; 3 class Caldemo { 4 5 public void Say () { 6 7 System.out.println ("sqrt" +sqrt (5.18 9 } 10 }
However, if users have less use of the code, it is not recommended to use the new usage. In this case, it is misleading to think that the user has defined a sqrt () method.
Note: To use static import sparingly, it will make the program difficult to understand and maintain, destroy all the imported static members of the namespace, and undermine the readability of the program
-----------Android Training, Java training, Java Learning Technology blog, look forward to communicating with you! ------------
Dark Horse Programmer--java Basics---about static (static) knowledge