Comparison between PHP static and Java static

Source: Internet
Author: User
The comparison between PHP static and Java static is also called class variables/class methods in Java. they exist in the memory stack and have only one copy. And objectDirect access

Public class MyStatic {public static String className = "MyStatic"; public static void getClassName () {// of course you can also access it by writing className directly, the premise is that no local variable name in this function overwrites the static variable name System. out. println (MyStatic. className);} public static void main (String [] args) {// class access static variable System. out. println (MyStatic. className); // object access static variable System. out. println (new Mystatic ()). className); // class access static method Mystatic. getClassName (); // static method for object access (new Mystatic ()). getClassName (); // ========= (new Mystatic ()). className = "new class name"; // ============/// class access static variable System. out. println (MyStatic. className); // object access static variable System. out. println (new Mystatic ()). className); // class access static method Mystatic. getClassName (); // static method for object access (new Mystatic ()). getClassName ();}}

Note: java static variables do not prohibit object access. However, unlike PHP, PHP static methods can be called by objects, but static variables of classes can only be accessed by class methods, objects cannot be accessed directly.

Class Mystatic {public static $ className = _ CLASS __; public static function getClassName () {echo self ::$ className ;}} echo Mystatic ::$ className; Mystatic :: getClassName (); // but (new Mystatic ()-> className; // error does not have permission // but the following is acceptable (new Mystatic ()) -> getClassName (); // only one static copy is available. you can connect them together and modify Mystatic: $ className = "new class Name"; (new Mystatic () synchronously ()) -> getClassName ();

Therefore, PHP Objects must rely on interfaces to write out methods for accessing static variables in the class if they are to be used for static variables of the runtime class, rather than using the. syntax class and objects as in Java.

Related Article

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.