Java Tour (Fri)--this,static, keyword, main function, encapsulating tool class, generating Javadoc manual, static code block
Have a lot of weekend, continue to study
One. this keyword
Used to distinguish between local variables and member variables with the same name
Features of this
This represents the object of this class
It's in our set method.
publicvoidsetName(String name) { this.name = name; }
This represents the reference of the function to which he is a dependent object
Now we have a demand here.
//public class class name Public class Hellojjava { //public static no return value the Main method array Public Static void Main(string[] str) {/** * Requirements: Defines a function that is used to compare the same age, i.e. whether it is a peer/person P1 =NewPerson ( -); person P2 =NewPerson ( -);Booleanb = P1.compare (p2); System.out.println (b); }}class Person {Private intAge//One is initialized to be of age Public Person(intAge) { This. Age = Age; } Public int Getage() {returnAge } Public void Setage(intAge) { This. Age = Age; } Public Boolean Compare(Person P) {return This. age = = P.age; }}
The result must be false.
As you can see, when defining a function in a class, the function's object is used internally, and this is used to represent the type of object used internally by this function.
Let's take a look at a little bit of knowledge, and we'll see a demand
//public class class name Public classHellojjava {//public static no return value the Main method array Public Static void Main(string[] str) {//constructor can only be used with this statement}}class Person {Private intAgePrivateString name; Public Person(intAge) { This. Age = Age; } Public Person(intAge, String name) { This(age);//On behalf of the age of P This. name = name; }}
This () function, where we should note that this statement can only be placed on the first line of the constructor to initialize the object.
Two. Static keyword
Static, let's look at a small example
//public class class name Public classHellojjava {//public static no return value the Main method array Public Static void Main(string[] str) {Person p =NewPerson (); P.age = -; P.show (); }}class Person {intAge String name ="LGL";StaticString Country ="cn"; Public void Show() {System. out. println ("Your name:"+ name +"This year:"+ Age +"Old"+"Nationality:"+ country); }}
Static is a modifier, is a modifier member variable, member function of the keyword, after being statically decorated, he is not in memory, is extracted separately, everyone can access, static decoration content is shared by the object
When a member is statically modified, it is called in addition to being called by the object, and can be called directly by the class name: the class name. Static members
System.err.println(Person.country);
As you can
In this way, we can summarize the characteristics of static
The difference between an instance variable and a class variable:
- Storage location
- Class variables exist in the method area as the class is loaded, and the message as the class's message
- Instance variables exist in heap memory as objects are established
- Life cycle
- Class variables have the longest life cycle and disappear with class hours
- The life cycle of an instance variable disappears as the object disappears
Considerations for using static variables
- Static methods can only access static members
- Non-static method methods can access static or non-static
- The This,super keyword cannot be defined in a static method because static takes precedence over the existence of the object, which is not allowed in static methods to appear in their
- The main function is static
Static Advantages and Disadvantages
- Benefit: Save space by storing individual controls on the shared data of an object, it is not necessary to store each object again, or it can be called directly by the class name
- Cons: Too long life cycle, limited access (static is good, only access to static)
Here is to say a little bit of knowledge:
Main function
Main function Everyone should be familiar with it.
The main function is a special function that can be called by the JVM as the entrance to the program.
- Public: The delegate's access rights are the largest
- Static: Represents the main function that exists as the class loads
- void: The main function does not have a specific return value
- Main: Not a keyword, but a special word that can be identified by the JVM
- parameter of the function: The parameter type is an array, the element in the array is a string, an array of type string
The format of the main function is fixed, and the JVM recognizes
OK, understanding the main function, we go back to static, when to use static?
To do this in two ways, because statically decorated content has member variables and functions
Package Tool Class
We can see how we're going to find the maximum value.
//public class class name Public class Hellojjava { //public static no return value the Main method array Public Static void Main(string[] str) {int[] arr = {0,9,0,6,2,8};intmax = Getma (arr); System.out.println ("Maximum value:"+ max); }/** * Find the maximum value of an array * @param arr * @return */ Public Static int Getma(int[] arr) {intMax =0; for(inti =0; i < arr.length; i++) {if(Arr[i] > Arr[max]) {max = i; } }returnArr[max]; }}
It is really convenient for us to extract this method, but what if other classes have arrays that require the maximum value? At this point we can encapsulate it as a tool class.
- Each application has a common part that can be extracted, individually encapsulated for use, so we can write:
/** * Array tool * * @author LGL * */ Public class arraytools { /** * Find the maximum value of an array * * @param arr * @return */ Public Static int Getmax(int[] arr) {intMax =0; for(inti =0; i < arr.length; i++) {if(Arr[i] > Arr[max]) {max = i; } }returnArr[max]; }/** * Find the minimum value of an array * * @param arr * @return */ Public Static int Getmin(int[] arr) {intMin =0; for(inti =0; i < arr.length; i++) {if(Arr[i] < arr[min]) {min = i; } }returnArr[min]; }}
The methods of obtaining the maximum and minimum values are encapsulated and modified with static so that
//public class class name Public classHellojjava {//public static no return value the Main method array Public Static void Main(string[] str) {int[] arr = {0,9,1,6,2,8};intmax = Arraytools.getmax (arr);intmin = Arraytools.getmin (arr); System. out. println ("Maximum value:"+ max); System. out. println ("Minimum value:"+ min); }}
We can get it straight.
Although we can use these methods with Arraytools objects to operate on an array, there are some problems.
- Objects are used to encapsulate data, but Arraytools object disease does not encapsulate unique data
- Each method that operates an array does not use the unique data in the Arraytools object
This time we can consider, let the program more rigorous, do not need the object, you can define the Arraytools method is static, directly through the class name can be called
Generate Javadoc Manual
When we write a tool class, it can be widely circulated, but, people do not know what to write Ah, so, we write a manual is necessary, first back to our tool class
/** * Array tool * * @author LGL * */ Public class arraytools { /** * To find the maximum value of an array * * @param arr * received array * @return */ Public Static int Getmax(int[] arr) {intMax =0; for(inti =0; i < arr.length; i++) {/** * Two number comparison * / if(Arr[i] > Arr[max]) {max = i; } }/** * Returns the maximum value */ returnArr[max]; }/** * To find the minimum value of an array * * @param arr * receives an array * @return */ Public Static int Getmin(int[] arr) {intMin =0; for(inti =0; i < arr.length; i++) {/** * Two number comparison * / if(Arr[i] < arr[min]) {min = i; } }/** * Returns the minimum value */ returnArr[min]; }}
As you can see, we have added a lot of comments, and now we can go to build, we see a file under the bin file of the JDK installation directory called Javadoc, we need him, we can in the Java class directory
// myhelp:文件夹 -author:作者,可以不写-d-author ArrayTools.java
After the build we open index.html
Static code block
Let's talk about a little bit of knowledge static code fast, let's look at the format first
class StaticDemo { static { //静态代码快 }}
Static code block features: executes as the class is loaded and executes only once
//public class class name Public classHellojjava {Static{//Static code fastSystem.err.println ("B"); }//public static no return value the Main method array Public Static void Main(string[] str) {NewStaticdemo (); System. out. println ("Over"); }Static{//Static code fastSystem.err.println ("C"); }}class Staticdemo {Static{//Static code fastSystem.err.println ("a"); }}
We can guess, what is the order of execution? The static method takes precedence over the Mian method from top to bottom, so it is B and then goes to the main method output A,over
All right, this is the first place to be here. Add Group: 555974449
Java Tour (Fri)--this,static, keyword, main function, encapsulating tool class, generating Javadoc manual, static code block