Why Java does not have global variables

Source: Internet
Author: User

Look at a python-written Java that actually writes out the global variables.

If you are familiar with PY oop, you can also know that there are no global variables in the class.

The variables in the class are divided into three kinds of attribute instance properties local variables, class attributes can also be called static property static variables, instance properties can also be similar to the meaning of member variables. But there is no global variable.

Why do some languages have global variables? JS py php all have.

In py, if you are using process-oriented programming, multiple functions manipulate a variable unless you frequently pass and return the arguments in multiple functions. Otherwise, you need to use global variables, because the function is at the module level of the namespace, then the function of the write variable outside the module can only be written below the module, the variable is called the global variable.

In the case of OOP programming, the namespace of the method is not under the module, but in the class name, so it is only necessary to write something that would have to be written as a global variable in a process-oriented way, as an instance or class attribute. This allows access to this variable in multiple methods. Because OOP sinks the namespace by one level, the module-class-method level three differs from the module-function level Two, which is oriented to the process.

Java is a language that enforces OOP, and all classes are namespaces, and of course there is no need for a global variable.

This is the same as I have previously confirmed, do not use global variables, especially global variable variables, and declare gloabl everywhere, really is the egg pain, a little bit of oop consciousness. Using OOP can completely eliminate global variables. been at ease for more than 10 years. Repeating a process-oriented low force code is not a good thing.

Why Java does not have global variables

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.