Home > Back-end >  Java main function is a static function, only can call a static method, why in the class instance me
Java main function is a static function, only can call a static method, why in the class instance me

Time:03-04

Java main function is a static function, only can call a static method, why in the class instance methods is not a static may also directly in the main function call? As shown in the following

why can call in the MAIN function is not a static call function?

Public class AccessProperty {
Static int I=47;//define a static member variable

Public void method call () {//define members
System. The out. Println (" call call () method ");

}

Public AccessProperty () {//define a constructor
}

Public static void main (String [] args) {//define the primary methods
AccessProperty t1=new AccessProperty ();//create an object
AccessProperty t2=new AccessProperty ();//create another object
T1. The call ();//use the first object call a class member method
T2. The call ();//using the second object call a class member method
}
}

CodePudding user response:

refer to the original poster eisldkw response:
Java main function is a static function, only can call a static method, why in the class instance methods is not a static may also directly in the main function call? As shown in the following

why can call in the MAIN function is not a static call function?

Public class AccessProperty {
Static int I=47;//define a static member variable

Public void method call () {//define members
System. The out. Println (" call call () method ");

}

Public AccessProperty () {//define a constructor
}

Public static void main (String [] args) {//define the primary methods
AccessProperty t1=new AccessProperty ();//create an object
AccessProperty t2=new AccessProperty ();//create another object
T1. The call ();//use the first object call a class member method
T2. The call ();//using the second object call a class member method
}
}



Why not call on his own way? Please look at the static can only call methods of static, not internal cannot be used

CodePudding user response:

Instance variables was established within a method, of course, can be called directly,

CodePudding user response:

You are new, then you call direct call? New to the call object method is not normal operation? Don't know why you ask this.

CodePudding user response:

Static methods can be called object. Non-static methods , can not directly call non-static methods
Call the t1 is in the main. Call is no problem, but can be not the direct call call, pay attention to the difference between

CodePudding user response:

Public class TestOne
{
Public static void main (String [] args)
{
//TODO Auto - generated method stub
Print ();
}

Public static void print ()
{
System. The out. Println (" hello world ");
}
}
This is called directly, the static method can not by the method of direct access to the static object, but a static method cannot access non-static methods directly, here involves the implied this reference, it is recommended that the investigating
And you are to create objects, object can be used to access this method, of course, he isn't static can be used in class to access the object

CodePudding user response:

Static memory space is fixed, relatively less resources, and the instance and an instance is open up a new memory, consuming resources
Static methods belong to the class, before class instantiation can use;
Non-static methods can access any members of the class, and a static method can only access static members in class;
Because static methods can be used in front of the class instantiation, and in the class non-static variables must be instantiated to allocate memory;
In only the static internal static variables and other static method! And static method cannot be used in this keyword, because it belongs to the whole class;
Static method is of high efficiency than on the instantiation and the shortcoming of static method is not automatically destroyed, whereas the instantiation of could be destroyed;
The static method and static variables are created always use the same piece of memory, and using instance methods can create multiple memory,
Major difference: the static method can use, before creating objects non-static methods must pass the new out calls,
Static methods can be the name of the class: : method of direct call, common methods need to create an instance, who is one of new objects, and then through the object name - & gt; The method name to call
Static class can contain static members, otherwise it will throw a compiler error; But not a static class can contain static members also can contain static members
Static class cannot be instantiated, the reason cannot be instantiated, because static class will lead to the c # compiler will the class mark for the abstract and sealed at the same time, and the compiler will not in the type
To generate an instance constructors, resulting in a static class cannot be instantiated; Non-static classes can, and the static member access can only be accessed by class, because the static member belongs to the class,

https://blog.csdn.net/weixin_44535476/article/details/90245351
  • Related