In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you how to parse satic keywords, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Static keyword parsing in Java
The static keyword is a difficult keyword that many friends encounter when writing and reading code, and it is also one of the knowledge points that interviewers in major companies like to ask during interviews. The following will first talk about the usage of the static keyword and some common misunderstandings, and finally list some common questions about static in the written interview.
I. the purpose of the static keyword
There is a passage on page P86 of "Java programming ideas":
"static methods are methods without this. Non-static methods cannot be called within static methods, and vice versa. And you can call static methods only through the class itself without creating any objects. This is actually the main purpose of static methods."
Although this passage only illustrates the special features of the static method, you can see the basic function of the static keyword. In short, it can be described in one sentence:
It is convenient to call (methods / variables) without creating an object.
Obviously, methods or variables modified by the static keyword do not need to rely on the object for access, as long as the class is loaded, it can be accessed through the class name.
Static can be used to modify class member methods, class member variables, and you can write static code blocks to optimize program performance.
1) static method
Static methods are generally called static methods. Because static methods can be accessed without relying on any object, there is no this for static methods, because they are not attached to any objects. Since there are no objects, there is no this. And because of this feature, the non-static member variables and non-static member methods of the class cannot be accessed in static methods, because non-static member methods / variables must rely on specific objects before they can be called.
Note, however, that although non-static member methods and non-static member variables cannot be accessed in static methods, static member methods / variables can be accessed in non-static member methods. Take a simple example:
In the above code, because the print2 method is independent of the object, it can be called directly with the class name. If you can access a non-static method / variable in a static method, then you have the following statement in the main method:
MyObject.print2 ()
At this point, there are no objects, and the str2 does not exist at all, so there will be contradictions. The same is true for methods, because you can't predict whether non-static member variables are accessed in print1 methods, so access to non-static member methods is also prohibited in static member methods.
For non-static member methods, access to static member methods / variables is obviously unrestricted.
Therefore, if you want to call a method without creating an object, you can set the method to static. Our most common static method is the main method, and it is now clear why the main method must be static. Because the program does not create any objects when it executes the main method, it can only be accessed through the class name.
Also remember that the constructor of a class is actually a static method even if it is not explicitly declared as static.
2) static variable
Static variables are also called static variables. The difference between static variables and non-static variables is that static variables are shared by all objects, have only one copy in memory, and are initialized only when the class is first loaded. Non-static variables are owned by the object, are initialized when the object is created, there are multiple copies, and the copies owned by each object do not affect each other.
The initialization order of static member variables is initialized in the order defined.
3) static code block
Another key function of the static keyword is to form static blocks of code to optimize program performance. Static blocks can be placed anywhere in a class, and there can be multiple static blocks in a class. When the class is first loaded, each static block is executed in the order of the static blocks, and only once.
The reason why the static block can be used to optimize program performance is that it is executed only once when the class is loaded. Let's look at an example:
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
Class Person {
Private Date birthDate
Private static Date startDate,endDate
Static {
StartDate = Date.valueOf ("1946")
EndDate = Date.valueOf ("1964")
}
Public Person (Date birthDate) {
This.birthDate = birthDate
}
Boolean isBornBoomer () {
Return birthDate.compareTo (startDate) > = 0 & & birthDate.compareTo (endDate)
< 0; } } 因此,很多时候会将一些只需要进行一次的初始化操作都放在static代码块中进行。 二.static关键字的误区 1.static关键字会改变类中成员的访问权限吗? 有些初学的朋友会将java中的static与C/C++中的static关键字的功能混淆了。在这里只需要记住一点:与C/C++中的static不同,Java中的static关键字不会影响到变量或者方法的作用域。在Java中能够影响到访问权限的只有private、public、protected(包括包访问权限)这几个关键字。看下面的例子就明白了:The error "Person.age is not visible" is prompted, which means that the static keyword does not change the access rights of variables and methods.
two。 Can I access static member variables through this?
Although there is no this for static methods, can static member variables be accessed through this in non-static methods? Let's take a look at the following example. What is the output of this code?
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
Public class Test extends Base {
Static {
System.out.println ("test static")
}
Public Test () {
System.out.println ("test constructor")
}
Public static void main (String [] args) {
New Test ()
}
}
Class Base {
Static {
System.out.println ("base static")
}
Public Base () {
System.out.println ("base constructor")
}
}
Base static test static base constructortest constructor
As for why this result is not discussed, let's first think about the specific execution process of this code. At the beginning of execution, we should first find the main method, because the main method is the entrance to the program, but before executing the main method, we must first load the Test class, and when loading the Test class, we find that the Test class inherits from the Base class, so we will load the Base class first. When loading the Base class, we will find that there is a static block. The static block is executed. After the Base class is loaded, it continues to load the Test class, and then finds that there is also a static block in the Test class, and executes the static block. After the required classes are loaded, the main method is executed. When new Test () is executed in the main method, the parent class's constructor is called first, and then its own constructor is called. As a result, the above output appears.
two。 What is the output of this code?
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
thirty-five
Public class Test {
Static {
System.out.println ("test static 1")
}
Public static void main (String [] args) {
}
Static {
System.out.println ("test static 2")
}
}
Test static 1 test static 2
Although there are no statements in the main method, it will still be output, for the reasons described above. In addition, static blocks can appear anywhere in the class (as long as they are not inside the method, remember, not inside any method), and execution is performed in the order of the static blocks.
On how to carry out satic keyword parsing to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.