About scopes for "Java"

Source: Internet
Author: User

There are three main types of variables in the Java language: member variables, static variables (global variables), local variables

The scope is determined by the location of the {}, which determines the visibility and life cycle of the variable name it defines

    • Member variables

      Member variables have the same scope as the instantiated objects, and when the class is instantiated, the member variable allocates space in memory and initializes it, and the lifetime of the member variable ends when the life cycle of the instantiated object ends

    • Static variables (global variables)

      Modified by the static keyword, the static variable is not dependent on a particular instance, but is shared by all instances, and as long as a class is loaded, the JVM allocates storage space for the static variables of the class. So, we can access static variables by class name and variable name.

    • Local variables

      Its scope and visibility are within the {} in which it resides


4 Scopes for member variables:

    • Public

      This member variable or method is visible to all classes and objects and can be accessed and invoked directly by them

      (This class, this package, sub-category, other package)

    • Private

      The member variable or method is private, and only this class has access to it

      (In this category)

    • Protected

      The member variable or method is visible to itself and its subclasses

      (This class, the package, the sub-category)

    • Default

      This member variable is visible only to itself and to the class it is in the same package

      In the case of an inheritance relationship, when the parent class is in the same package as the child class, the child class has access to the default member variable or method in the parent class, and the subclass does not have access to the default variable or method in the parent class when the parent class is not in the same package as the child class.

      (This class, the package),


Note: Peivata, protected cannot be used to modify classes, only (public, abstract, final) that can be used to modify classes

Instance methods can call the class method of this class directly

About scopes for "Java"

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.