In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the role and scope of various modifiers in java". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
access modifiers
private
default
protected
public
scope access modifier\scope is in the same package other classes in other packages subclass in other packages nonsubclass private can be accessed no can not default can not be protected can not be public can be can be can be
private
Properties and methods modified by private cannot be accessed by other classes, and subclasses cannot inherit or access them. It can only be accessed within the class.
default
A variable or method without an access modifier can be accessed by its class and can be accessed or inherited by other classes in the same package. But it cannot be accessed by other packages.
protected
Protected methods and properties can be accessed and inherited within the same package. Within different packages, subclasses can inherit, and non-subclasses cannot access.
public
Methods and attributes are prefixed with public modifiers and can be accessed by any class in the package.
In addition, classes must be declared public in order to be imported by other packages. Class names must be the same as file names for classes modified by public.
Characteristics of static modifiers
static is a modifier used to modify members (member variables and member functions)
1. Static members load as the class loads.
2. Static members take precedence over object existence.
3. Static members are shared by all objects
4. Static members have one more call mode, which can be called directly by the class name.
Precautions for static use
1. Static methods can access only static members, and non-static methods can access both static and non-static members.
2. Static methods cannot define the this, super keyword. Because this represents the object. When static exists, there may be no objects. So when static methods run, this is represented by nothing. Simply put, advanced memory data cannot access data from backward memory, but backward memory data can access data from advanced memory.
3. The main function is static
Advantages and disadvantages of static
Pros: Static members have one more way to call. Can be called directly by class name Format: class name. static member. It can also be called by objects.
Disadvantages: Static methods can only access static members, there are access limitations.
Statically decorated data objects share data stored in the static area of the method area.
Non-static data is data specific to each object. The object is stored in the heap memory to which it belongs.
When a member variable is statically modified, it differs from a non-static member variable.
1. Static variables are also called class variables, i.e. variables that can be called directly by class names. This variable is of the class to which it belongs. A non-static variable becomes a member variable, or instance variable, that is called by an object and belongs to a specific object.
2. Static variables are loaded as classes are loaded, which means they disappear as classes disappear. Longest life cycle. Instance variables, loaded as objects are created, disappear as objects disappear. Exist according to the life cycle of the object.
3. Static variables are stored in the static section of the methods section. Instance variables exist in the heap memory to which the object belongs.
4. Static variable data, shared by all objects. Instance variables are unique data in an object
"Java in the role of various modifiers and what is the scope" of the content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.