------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
One: Overview
1. Static keyword
Characteristics:
1. To implement object sharing of common data in objects, this data can be modified statically.
2. Statically decorated members can be called directly by the class name, that is, static members have a more than one invocation method.
3. Static loads with the load of the class, and takes precedence over the existence of the object.
Disadvantages:
1. Some data is an object-specific data, and can not be statically modified, because in that case, the unique data will become the object's shared data, so that the description of the thing is a problem, so in the definition of static, it must be clear that the data is shared by the object.
2. Static methods can access only static members, not non-static members, because static methods load with precedence over the existence of objects, so there is no way to access the members of the object.
3. The This, super keyword cannot be used in a static method.
This is not possible because this represents an object and is static at the time that there is no object.
4. The main function is static.
Two: Realize
1. When are static members defined?
The members are divided into two types:
1. member variables (static when data is shared)
Whether the data for this member variable is the same for all objects.
If so, then the variable needs to be statically decorated because it is a shared data
If not, then say that this is the object's unique data, to be stored in the object
Static members are stored in the method area, or in a shared area.
2. member functions (defined as static when no specific data is called in the method)
What if the member function needs to be statically decorated?
Whether the object's unique data is accessed within the function:
If there is access to unique data, the method cannot be statically modified
If no unique data has been accessed, then this method needs to be statically modified.
The difference between a member variable and a static variable:
1. The member variable belongs to the object, so it is also called an instance variable.
Static variable belongs to class, so also called class variable
2. Member variables are stored in heap memory
Static variables are stored in the method area
3. Member variables are created with object creation and disappear as objects are recycled
Static variables exist as classes are loaded, and the age of this kind disappears.
4. Member variables can only be called by an object
A static variable can be called by an object, or it can be called by a class.
Therefore, member variables can also be called object-specific data, and static variables are called shared data for objects.
Three: Code implementation
public class Araaytool{public static int Getmax (int [] arr) {int max = 0;for (int i=0; i<arr.length-1; i++) {if (arr[i) ; Arr[max]) {max = i;}} return Max;} public static int getmin (int [] arr) {int min = 0;for (int i=0; i<arr.length-1; i++) {if (Arr[i] < arr[min]) {min = i;}} return min;} public static void Selectsort (int. [] arr) {for (Int. i=0; i<arr.length-1; i++) {for (int j=x+1; j<arr.length; J + +) {if (a[ I]>a[j]) {swap (arr,i,j); {}}} public static void Bubbletsort (int. [] arr) {for (Int. i=0; i<arr.length-1; i++) {for (int j=0; j<arr.length-x-1; J + +) {if (A[j]>a[j+1]) {swap (arr,j,j+1); {}}}public static void swap (int [] arr, int a, int b) {int temp = Arr[a];arr[a] = arr[b];arr[b] = temp;} public static void print (int. [] arr) {System.out.print ("["); for (int i=0; i<arr.length; i++) {System.out.print (arr[i]+ ",");} System.out.print ("]");}} public class Arraydemo{public static void Main (String [] args) {int [] arr = new int [] {1,2,6,9,5,8,4}; Arraytool.getmax (arr); Arraytool.selectsort (arr); ArraYtool.print (arr);}} static applications.
Each application has a common function,
These functions can be extracted, individually encapsulated.
For reuse.
Although you can use these tool methods to manipulate an array by building Arraytool objects.
Found the problem:
1, the object is used to encapsulate the data, but the Arraytool object does not encapsulate the unique data.
2, each method of manipulating an array does not use the unique data in the Arraytool object.
At this point, consider, let the program more rigorous, is not required objects.
The methods in Arraytool can be defined as static. It can be called directly from the class name.
It is easy to use a method after it is static, but it can also be used by other programs to establish objects.
To be more rigorous, force the class to fail to establish an object.
You can do this by privatizing the constructor
Four: Help document making
Command format: javadoc-d folder name
Document Format:
/**
* Class Description
* @author Author name
* @version Version number
*/
/**
* Method Description
* @param parameter description
* @return Return value description
*/
Five: A detailed description of the main function
Structure of the main function: public static void Main (String [] args)
Public: Maximum access rights
Static: No object required, can use class name directly
void: The main function has no return value
Main: Main function-specific name
(string [] args): The parameter of the main function, is a parameter of a string group type, and executes only once, and the new object executes only once, and if the main function is in the same class, it takes precedence over the main function execution.
By default, the JVM passes a string array of length 0, and when the JVM calls the main method, the actual parameter passed is string[0];
public class Tooldemo{public static void Main (String [] args) {System.out.println (args);}} output [[Email protected]public class tooldemo{public static void Main (String [] args) {System.out.println (args.length);}}
//The length of the output is 0, which means the default is 0
Static code blocks, construction code blocks, constructors ' execution order: Static code blocks, construction code blocks, constructors
public class Tooldemo{public static void Main (String [] args) {Staticdemo (3);//execution order B c D//There is no object defined, so the constructor does not execute, there is a constructor//code block execution }}public class Staticdemo{staticdemo () {System.out.println ("a");} Static{system.out.println ("B");} {System.out.println ("C");} static (int n) {System.out.println ("D");}}
Dark Horse Programmer--java Basics-Object Oriented 2