Java static identifier static explanation

Source: Internet
Author: User

1, static modification of the variable is used to be referred to as statically variable, static modification of the method is referred to as a stationary method, static modified code block is called a block.

1) Static variable

Static variables are also known as static variables, except that the static variables are shared by all objects, only one copy in memory, and are initialized only when the class is first loaded. Instead of static variables, which are owned by an object, are initialized when the object is created, there are multiple copies, and the replicas owned by each object do not affect each other.

The initialization order of static member variables is initialized in the order in which they are defined.

1  Public   Static String sr = "Woshidashabi";

2) static method

Static methods are commonly referred to as static methods, and because they can be accessed without relying on any object, there is no this for static methods because it is not attached to any object, and since there are no objects, this is not the case. And because of this feature, non-static member variables and non-static member methods of the class cannot be accessed in a static method, because non-static member methods/variables must be dependent on a specific object to be able to be called.

1  Public Static void f () {...};

3) Static code block

Another key function of the static keyword is to form static blocks of code to optimize program performance. A static block can be placed anywhere in the class and can have more than one static block in the class. When the class is first loaded, each static block is executed in the order of the static blocks and is executed only once.

1    Static {2         startdate = date.valueof ("1946"); 3         EndDate = date.valueof ("1964"); 4     }

Reference blog:http://www.cnblogs.com/dolphin0520/p/3799052.html (big font more conspicuous)

2. A static variable of Java is also called a class variable, which begins with the creation of a class and ends with the extinction of a class. A non-static variable is called an instance variable, which begins with the creation of an instance of a class and the demise of an instance of a class.

Static variables are shared by all instances.

3. Why can static classes call methods directly in Java?

Because the static method of the class is automatically placed in memory when the program starts, it is the public memory of the program (but only accessible), and the class name here you can understand as the namespace. The first popular analysis, we think of the class as a house. The house has furniture, chairs and the like, there are people in the house.
All the people in the House should have a set of furniture. In other words, the furniture is unique, if a piece of furniture is broken, then everyone can not use.
Let's take a look at the definition, a static variable in Java is also called a class variable, which begins with the creation of the class and ends with the extinction of the class. A non-static variable is called an instance variable, which begins with the creation of an instance of a class and the demise of an instance of a class. Static variables are shared by all instances. As the above example, the bench is a class variable, they are added after the house has been built, and basically are unique. People are the equivalent of examples, everyone can use these furniture, but if the furniture once damaged, it is bad, or you take a piece of furniture away, then all the people do not use this furniture, the house does not exist this furniture.
But the house can enter a lot of people, can enter Zhang San, also can enter John Doe. So these people are examples of class objects, and the clothes they wear can be called instance variables.
If there is a static variable in a class, the program will first load the static variable into memory, that is, in the heap to open a region dedicated to storage. The static variable is always there, no matter how many classes of objects you are new. In other words, a static variable will not open a new memory space for the variable once the class is initialized. And each new class of objects, the system will be re-
Heap memory opens up a new space to hold the instance object of the class, and a new reference variable in the stack to point to it.
Static methods are similar, but one thing to emphasize is that static methods cannot invoke non-static methods. Because the static modified method is first loaded into memory by the ClassLoader object, it is possible that other non-static methods or variables have not been loaded. Like I now want to make buns, now flour is static modification, the first has to get you around, but because the stuffed bun is not static modification, so may bun stuffing has not shipped over, you said how to do the bun it.
The static is modified with the initialization of the class is generated, in the heap memory has a dedicated area to store, so only the class name point method name or variable name can be. Rather than static, it must be adjusted by the object of the class. It's like you want a red dress, you have to get it from the person in red, so you have to find the person in red, the instance object of the class, and if you're going to find a table, and the table is in the room, you just walk through the room and get it.

4, the use of static keywords

In the Java programming thought P86 page, there is a passage:

The static method is the method without this. A non-static method cannot be called inside a static method, which in turn is possible. It is also possible to invoke the static method only through the class itself, without creating any objects. This is actually the primary use of the static method. ”

Makes it easy to invoke (method/variable) without creating an object.

In fact, if you want to invoke non-static methods in the static method, there is a way to solve.

1  Public classstaticmethodtest{2     voidNonstaticmethod () {3System.out.println ("This is copy someone else's code");4     }5 //Nonstaticmethod () is a non-static method6     7    Static voidStaticmethod (staticmethodtest s) {8System.out.println ("Too lazy to write."));9 S.nonstaticmethod ();Ten     } One  /*** We call the Nonstaticmethod () method in the Staticmethod () method, where we do a compromise, we cannot create an object instance to invoke a non-static method when the static method is loaded. But here we are just creating a reference and not creating an instance. So there is no violation of Java syntax, so compile can pass, when there is a call to this static method, the external will pass in an instance of the reference, and then through the instance to invoke the method Nonstaticmethod (). *********/ A  -  Public Static voidMain (string[] args) { -Staticmethodtest sobj=NewStaticmethodtest ();//Create an instance theStaticmethod (Sobj);//call a static method and pass in a reference to an instance.  -     } -}

Note: Java references can exist separately, not necessarily dependent on the instance.

1 String   sr;   Create a reference with the name SR 23 String mm= "SM"; // create an instance with a reference to mm 4 5 sr=mm; // assign a reference to the MM corresponding to the SR, which is just a copy of the reference, and does not manipulate SM. 

Does the 5.static keyword change the access rights of members in a class?

Some beginner friends confuse the static in Java with the function of the static keyword in C + +. Here's just one thing to keep in mind: unlike static in C + +, the static keyword in Java does not affect the scope of a variable or method. The only private, public, protected (including package access rights) keywords that can affect access in Java.

6. Can I access static member variables through this?

1  Public classMain {2     Static intValue = 33;3  4      Public Static voidMain (string[] args)throwsexception{5         NewMain (). Printvalue ();6     }7  8     Private voidPrintvalue () {9         intValue = 3;TenSystem.out.println ( This. value); One     } A } -  - //The result is

This is the understanding of the main expedition this and the static. What does this represent? This represents the current object, so the current object is the object generated through new main () by calling Printvalue with new Main (). The static variable is enjoyed by the object, so the value of This.value in Printvalue is undoubtedly 33. The value inside the Printvalue method is a local variable and cannot be associated with this at all, so the output is 33. Always keep in mind that static member variables, while independent of the object, do not mean that they cannot be accessed through objects, and that all static and static variables can be accessed through the object (as long as access is sufficient).

Can 3.static act on local variables?

Static is a scoped local variable in C + +, but in Java it is important to remember that static is not allowed to decorate local variables. Do not ask why, this is the Java syntax of the provisions.

PS: The above are other people's content, afraid of their own trouble back and forth, concentrated on a bit. Specific blog address, the maximum font of this article ""

Java static identifier static explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.