Java static method and Java static method
We can see that the main method in Java is static. What are the characteristics of static?
Generally, an instance class requires instantiate to obtain an instance before using the method and variable.
The static method is not tied to this instance class, that is to say, the Instance obtained by instantiate does not use this method, but simply adds a method name by adding a class name.
Static method calls must also be static, because the non-static method does not exist before instantiate, so it cannot be found and used.
Therefore, when the java program runs, it will find the main function, and the method called in the main function must also be static.
Use static methods in JAVA
During programming, we must be clear about the differences between static methods and non-static methods of classes:
The most fundamental difference is from the compilation perspective:
1) The static method is to load directly to the memory during compilation (the memory area closest to the cpu is also called a stack), such as the public static main (args []) of the program. {} method. Can you provide an example?
Static methods cannot be instantiated or instantiated!
Currently, many Java integration tools write and compile
Therefore, you can use "Class Name" + "." + "static method name ()" to call
2) A non-static method (non-static method of the class) instantiates an object using the keyword "new". This object is placed in another heap area of the memory.
That is to say, during compilation, non-static methods must first instantiate an object of the class and call it through "Object Name" + "non-static method name;
Public class Student
{
Private String name;
/*************************************** *********************
* The following two methods encapsulate the attribute name of the non-static method of the class. Let's take a look at it in main ()
* How to call
**************************************** ********************/
Public set (string init_name)
{
This. name = init_name;
}
Publc String get ()
{
Return this. name;
}
// Constructor
Public Student (){}
Public Student (String init_name)
{
This. name = init_name;
}
// The following is a static method. Let's see how it is called in main ().
Public static void PrintClassName ()
{
System. out. print ("Class Name: Student ");
}
}
// MainClass
Pubic class MainClass
{
Public static void main (args [])
{
// Call the static method first without instantiating the object
Student. PrintClassName ();
// The object must be instantiated when a non-static method is called.
Student stu1 = new Student ();
Stu1.set ("Join ");
String stu1_name = stu1.get ();
}
}
What is the static method in Java?
The static method uses the public memory space, that is, all objects can be directly referenced. You do not need to create an object before using this method.
For example, I create a class with a static method:
Class Test {
Public static int z (int xx, int yy ){
Return xx + yy;
}
Public int zz (int xx, int yy ){
Return xx + yy;
}
}
When you use this class in a class that contains the main method, the reference method for the non-static and static methods is different, as shown below:
Import Test;
Public class mainClass {
Int sum;
Public static void main (String args []) {
Sum = Test. z (); // you can use this method or attribute directly using the class. method or attribute.
System. out. println (sum );
Test t = new Test ();
Sum = t. zz (); // because zz is not a static method, you can only create a t object using the Test class and then call the method of this object.
System. out. println (sum );
}
}
Ls also makes sense. Static methods can only be called by static methods.