Static Method (and Implicit Static Method)

Source: Internet
Author: User

The static method is the method without this. Non-static methods cannot be called within the static method. In turn, non-static methods can call static methods.
It is worth noting that non-static methods cannot be called directly within the static method, but non-static methods can be called by generating objects.
In the static method, non-static variables cannot be called. In fact, the principle is very simple, because static is global-oriented and equivalent to a global method, and cannot be used only for general variables in a class.
Static variables (global variables) cannot be declared in both static and non-static methods, because the variables declared in the method are local variables and it is impossible to define a global variable in them.

After understanding the above knowledge, you may find a special case: on the fourth version of the programming idea p157, the constructor is implicitly static! However, why can I directly call non-static methods in the constructor ??
Explicit and implicit static refers to the code that the system will execute during class instantiation.
For example

 

Class statictest... {// if there is no static keyword, it is called implicit static. After the system executes the explicit static statement, it will then execute the Implicit Static code.
...{
System. Out. println ("Hide static ");
Show (); // This method is not static, but there is no error here
}
Static... {// This static keyword is called explicit static. The system will first execute the explicit static code.
System. Out. println ("static ");
// Show (); // This method is not static, so an error occurs.
}
Public statictest ()... {// After the explicit and implicit static statements are executed, the system will execute the constructor code because the constructor is not static but will also be executed, just like the Implicit Static statements, so the book calls it Implicit Static.
System. Out. println ("constuctor ");
}
Public void show ()...{
System. Out. println ("show ");
}
Public static void main (string [] ARGs )...{
New statictest ();
}
}

So Explicit Implicit Static is only for the sequence of code execution by class instantiation. In a sense, all code without static can be called implicit static, implicit Static can call non-static members.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.