On the description of various modifiers and access modifiers in Java

  • 2020-05-30 20:05:28
  • OfStack

Classes in JAVA can only be public or package. This is logical: people define classes for others to use. If it is private, how can someone call it? But there is one inner class that can be defined as private. Strictly speaking, the inner class is not a kind of aboboard class, and the inner class is, in a sense, the spy and underground workers in the kingdom. The secret agents and underground workers played many roles for the kingdom, but they almost never dared to appear in public. Even if you have to show your face, you have to get the permission of the host (class) and the guidance of the guide (Interface) before you dare to come out in fear and trembling. Here are some general class modifiers and access modifiers:

Class 1:

Access modifier modifier class class name extends superclass name implement interface name (the locations of access modifiers and modifiers are interchangeable)

访问修饰符
名称 说明 备注
public 可以被所有的类访问使用 public类必须定义在类名相同的同名文件中
package 可以被同1个包中的类所使用 默认的访问权限,可以省略此关键字,可以定义在和public类的同1个文件中
修饰符
名称 说明 备注
final 使用此修饰符的类不能够被继承  
abstract 如果要使用abstract类,之前必须首先建1个继承abstract类的新类,新类中实现abstract类中的抽象方法。 类只要有1个abstract方法,类就必须定义为abstract,但abstract类不1定非要保护abstract方法不可

2: variable

There are no global variables in Java, only method variables, instance variables (non-static variables in the class), and class variables (static variables in the class).

Variables in a method cannot have access modifiers. So the following access modifier table is only for variables defined in the class.

When an instance variable is declared, if no initial value is assigned, it is initialized to either null (reference type) or 0, false (original type).

More complex instance variables can be initialized with an instance variable initializer, which is a block of statements contained in {} that runs when the constructor of the class is called, after the parent constructor, but before the constructor.

Class variables (static variables) can also be initialized through the class variable initializer, which is a block of statements contained in static{} and can only be initialized once.

访问修饰符
名称 说明 备注
public 可以被任何类访问  
protected 1.可以被同1个包的所有类访问。
2.可以被所有子类访问
即使子类不在同1个包中
private 只能被当前类中的方法访问  
缺省 只能被同1个包中的类访问 若子类不在同1个包中,不能访问

修饰符
名称 说明 备注
static 静态变量(又称为类变量,其它的称为实例变量) 1.可以被类的所有实例共享。
2.并不需要创建类的实例就可以访问静态变量
final 常量,值只能够分配1次,不能更改 1.注意不要使用const,虽然它和C、C++中的const关键字含义1样
2.可以同static1起使用,避免对类的每个实例维护1个拷贝
transient 告诉编译器,在类对象序列化的时候,此变量不需要持久保存 主要是因为改变量可以通过其它变量来得到,使用它是为了性能的问题
volatile 指出可能有多个线程修改此变量,要求编译器优化以保证对此变量的修改能够被正确的处理  

Method 3:

Access modifier modifier returns the type method name (parameter list) throws violation list

Class constructor methods cannot have modifiers, return types, and throws clauses

2. When the constructor method of a class is called, it first calls the constructor method of the parent class, then runs the initializer for instance variables and static variables, and then runs the constructor itself.

3. If the constructor method does not display a call to a parent constructor, the compiler will automatically add a default super() to it, and if the parent class does not have a default no-argument constructor, the compiler will report an error. super must be the first clause of the constructor method.

4. Notice how you understand the private constructor method.

Access modifier

名称 说明 备注
public 可以被任何类访问  
protected 1.可以被同1个包的所有类访问。
2.可以被所有子类访问
即使子类不在同1个包中
private 只能被当前类中的方法访问  
缺省 只能被同1个包中的类访问 若子类不在同1个包中,不能访问


修饰符
名称 说明 备注
static 静态方法(又称为类方法,其它的称为实例方法 1.提供不依赖于类实例的服务
2.并不需要创建类的实例就可以访问静态方法
final 防止任何子类方法重载 1.注意不要使用const,虽然它和C、C++中的const关键字含义1样
2.可以同static1起使用,避免对类的每个实例维护1个拷贝
abstract 抽象方法,类中已声明而没有实现的方法 不能将static方法、final方法或者类的构造器方法声明为abstract
native 用该修饰符定义的方法在类中没有实现,而大多数情况下该方法的实现是用C、C++编写的。 参见Sun的Java Native接口(JNI),JNI提供了运行时加载1个native方法的实现,并将其于1个Java类关联的功能
synchronized 多线程的支持 当1个此方法被调用时,没有其它线程能够调用该方法,其它的synchronized方法也不能调用该方法,直到该方法返回

4: interface

Access the modifier interface interface name extends interface list

1. An interface cannot define any implementation of the method it declares

2. Variables in an interface should always be defined as "public static final interface name", but these modifiers may not be included, as is the default of the compiler, and are displayed with modifiers mainly for program clarity

Access modifier

名称 说明 备注
public 可以被所有的类访问使用 public类必须定义在类名相同的同名文件中
package 可以被同1个包中的类所使用 默认的访问权限,可以省略此关键字,可以定义在和public类的同1个文件中


Related articles: