In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-06-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail the "Java variable types and modifier instance analysis", the content is detailed, the steps are clear, the details are handled properly, I hope this "Java variable type and modifier instance analysis" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.
1. Variable type: a. Definition:
It's similar to C++, except:
Type identifier [= value] [, identifier [= value]...]
Type is the Java data type. Identifier is the name of the variable. You can declare multiple variables of the same type with commas separated. Can be declared but not assigned.
b. Class variable (static variable) static variable:
Variables independent of the method are modified with static.
No matter how many objects a class creates, the class has only one copy of the class variable.
Static variables are created the first time they are accessed and destroyed at the end of the program. Has similar visibility to instance variables. However, in order to be visible to the consumers of the class, most static variables are declared as public types.
Static variables belong to a class, which does not produce objects, and can be called through the class name.
c. Local variable (local variable):
As with most languages, cycles that exist only within the scope, functions or methods are destroyed after they are run. (note that JAVA does not assign values to local variables by default, but when they are declared.)
d. Instance variable (member variable member variable):
Variables that are independent of the method, but without static modification. Instance variables are declared in a class, but outside methods, constructors, and statement blocks
The value of the instance variable should be referenced by at least one method, constructor, or statement block, so that the external can obtain the instance variable information in these ways.
Instance variables have default values. The default value for numeric variables is 0, for Boolean variables is false, and for reference type variables is null. The value of a variable can be specified at declaration or in the constructor
The instance variable belongs to the object of this class, which must be generated before the instance variable can be called.
two。 Modifier:
There are many kinds of modifiers, mainly non-access modifiers and access modifiers:
a. Access modifier:
Default (that is, nothing is written by default):
Visible in the same package without any modifiers. Most of the time, it defaults to permissions and inheritance similar to public.
Note: unlike default, public means that other classes anywhere can access it, while the default default can only be used within this package.
Private:
Visible in the same category. The strictest permissions can only be used by the class in which they are located, and even the subclasses cannot be used or inherited.
Note: classes cannot be modified (external classes)
Public:
Visible to all classes.
Note: unlike default, public means that other classes anywhere can access it, while the default default can only be used within this package.
Protected:
Visible to classes and all subclasses in the same package.
Subclasses are in the same package as base classes: variables, methods, and constructors declared as protected can be accessed by any other class in the same package
The subclass is not in the same package as the base class: then in the subclass, the subclass instance can access its protected method inherited from the base class, but not the protected method of the base class instance.
Note: classes (external classes) cannot be modified.
Note:
Methods declared as public in the parent class must also be public in the subclass.
Methods declared as protected in the parent class are either declared as protected or public in the subclass, and cannot be declared as private.
Methods declared as private in the parent class cannot be inherited.
b. Non-access modifier:
Static:
Divided into static variables and static methods, static variables are shown above in this article.
Static methods: the static keyword is used to declare static methods independent of the object. Static methods cannot use non-static variables of a class. The static method takes the data from the parameter list and calculates the data. (static variables are already in memory, not static until they are declared)
Static methods can be called directly through the class name without instantiating the class's object. It is more convenient to declare static methods in some utility classes.
Final:
Final means "final, final". Once a variable is assigned, it cannot be reassigned. It also divides variables and methods.
Instance variables decorated by final must explicitly specify the initial value. The final modifier is usually used with the static modifier to create class constants.
The final method in the parent class can be inherited by the child class, but cannot be overridden by the child class.
The main purpose of declaring the final method is to prevent the contents of the method from being modified.
Abstract:
Abstract classes cannot be used to instantiate objects, and the only purpose of declaring an abstract class is to extend it in the future. A class cannot be modified by both abstract and final. If a class contains abstract methods, the class must be declared as an abstract class, otherwise a compilation error will occur.
An abstract method is a method without any implementation, and the concrete implementation of this method is provided by subclasses. Abstract methods cannot be declared as final and static.
Any subclass that inherits an abstract class must implement all abstract methods of the parent class, unless the subclass is also an abstract class.
If a class contains several abstract methods, the class must be declared abstract. An abstract class may not contain abstract methods.
Synchronized:
Methods declared by the synchronized keyword can only be accessed by one thread at a time. The synchronized modifier can be applied to four access modifiers.
Transient:
When the serialized object contains an instance variable decorated by transient, the java virtual machine (JVM) skips that specific variable.
This modifier is included in the statement that defines the variable and is used to preprocess the data type of the class and variable.
Volatile:
A volatile-decorated member variable forces the value of the member variable to be reread from shared memory each time it is accessed by a thread. Also, when a member variable changes, the thread is forced to write back the change value to shared memory. So that at any time, two different threads always see the same value of a member variable.
A volatile object reference may be null.
Read this, the "Java variable types and modifier instance analysis" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.
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.