Differences between global variables, local variables, instance variables, and static variables

Source: Internet
Author: User
Tags define local

Global Variables (GLOBALS): also known as member variables, belong to variables defined in a class. Valid in the entire class.

Can be divided into:

(1). Class variables: Also known as static variables, with static decoration, can be called directly with the class name, all objects of the same class variable is shared the same block of memory space.

(2). Instance variables: Without static adornments, it can only be called through an object, and the same instance variable of all objects is shared with different memory spaces.

Local variables: refers to the variables that are defined in the method body and the parameters of the method, which are valid only within the method that defines it. is relative to the global variable .

When a global variable has the same name as a local variable: Local variables work within subroutines that define local variables, and global variables work in other places. Note: In object-oriented languages such as Java, it is also possible to have multiple local variable names. For example, the formal parameter of a method is the same as the name of the class member, and the formal parameter masks the class member, and if you want to access the class member, you should use the This keyword.

Differences between global variables, local variables, instance variables, and static 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.