Java修饰符汇总

 

Java修饰符可以分为访问修饰符和非访问修饰符两大类。

 

访问修饰符包括:

public、protected、default(friendly)、private

 

访问修饰符可以修饰的内容,如下表:

修饰符方法构造方法属性接口
public
protected××
default(friendly)×
private××

 

各访问修饰符的作用域,如下表:

作用域当前类同一个包子孙类其他包
public
protected×
friendly××
private×××

 

补充说明:

public:不同包的公共类相互访问时,需要先导入公共类所在的包。public的类必须保持类名和当前文件名一致,一个文件中只有一个public类。public类中的main()方法必须设置为public的。

//public可以修饰类、方法、构造方法、属性和接口
public class TestClass {
    public String testStr;

    public TestClass() {
        System.out.println("This is a public GouZao method");
    }

    public static void main(String[] str) {
        System.out.println("This the public main method");
    }
}

 

protected:可以被同一个包的其他类访问,也可以被不同包的子类访问。不能修饰类和接口。接口的成员变量和方法不能声明为protected。

//protected可以修饰属性、方法、构造方法
public class TestClass {
    protected String testStr;

    protected TestClass() {
        System.out.println("This is a public GouZao method");
    }

    protected void method() {
        System.out.println("This is a protected method");
    }
}

 

default(friendly):只能被同一个包的其他类访问。接口里的变量都隐式声明为public static final,而接口里的方法默认情况下访问权限为public。

//friendly可以修饰方法、构造方法、属性和构造器
class GouZaoQiOne {
    String testStr;
    GouZaoQiOne() {
        System.out.println("This is a default(friendly) GouZao method");
    }

    void method() {
        System.out.println("This is a default(friendly) method");
    }
}

 

private:只能被所属类访问。类和接口不能设置为private。

//private可以修饰方法、构造方法、属性
public class TestClass {
    private String testStr;

    private TestClass() {
        System.out.println("This is a public GouZao method");
    }

    private void method() {
        System.out.println("This is a protected method");
    }
}

 

非访问修饰符包括:

static、final、abstract、synchronized、transient、volatile、strictfp

 

非访问修饰符可以修饰的内容,如下表:

非访问修饰符方法属性构造方法
final×
static××
abstract××
synchronized×××
native×××
transient×××
volatile×××
strictfp×

 

static修饰符例子:

public class TestClass {
    public static void main(String[] str) {
        //static方法和属性,可以直接使用classname.variablename 和 classname.methodname 的方式访问
        System.out.println("This is the static shuxing: " + Test.str);
        Test.method();
    }
}

class Test {
    /**
     * 静态变量:static 关键字用来声明独立于对象的静态变量,无论一个类实例化多少对象,它的静态变量只有一份拷贝。
     * 静态变量也被成为类变量。局部变量不能被声明为 static 变量。
     */
    public static String str = "test str";

    /**
     * 静态方法:static 关键字用来声明独立于对象的静态方法。静态方法不能使用类的非静态变量。
     * 静态方法从参数列表得到数据,然后计算这些数据。
     */
    public static void method() {
        System.out.println("This is a static method");
    }
}

 

final修饰符例子:

public class TestClass {
    /**
     * final 变量能被显式地初始化并且只能初始化一次。被声明为 final 的对象的引用不能指向不同的对象。
     * 但是 final 对象里的数据可以被改变。也就是说 final 对象的引用不能改变,但是里面的值可以改变。
     * final 修饰符通常和 static 修饰符一起使用来创建类常量。
     */
    final String str = "testStr";
    public static final int MAX_INT = 10;
    static final String MESSAGE = "This is a message";

    /**
     * 类中的 final 方法可以被子类继承,但是不能被子类修改。
     * 声明 final 方法的主要目的是防止该方法的内容被修改。
     */
    public final void method() {}
}

/**
 * final 类不能被继承,没有类能够继承 final 类的任何特性。
 */
final class TestFinalClass {}

 

abstra修饰符例子:

/**
 * 抽象类不能用来实例化对象,声明抽象类的唯一目的是为了将来对该类进行扩充
 * 如果一个类包含抽象方法,那么该类一定要声明为抽象类
 * 抽象类可以包含抽象方法和非抽象方法,也可以不包含抽象方法
 */
abstract class TestAbstractClass {
    private String testStr;

    /**
     * 抽象方法是一种没有任何实现的方法,该方法的的具体实现由子类提供
     * 任何继承抽象类的子类必须实现父类的所有抽象方法,除非该子类也是抽象类
     */
    public abstract void method();
}

 

synchronized、transient、volatile修饰符例子:

class Test {
    /**
     * synchronized 关键字声明的方法同一时间只能被一个线程访问。
     * synchronized 修饰符可以应用于四个访问修饰符。
     */
    public synchronized void method() {}

    /**
     * 序列化的对象包含被 transient 修饰的实例变量时,java 虚拟机(JVM)跳过该特定的变量。
     * 该修饰符包含在定义变量的语句中,用来预处理类和变量的数据类型。
     */
    public transient int testNum = 100; // 不会持久化
    public int testInt = 10; //持久化

    /**
     * volatile 修饰的成员变量在每次被线程访问时,都强制从共享内存中重新读取该成员变量的值。
     * 而且,当成员变量发生变化时,会强制线程将变化值回写到共享内存。
     * 这样在任何时刻,两个不同的线程总是看到某个成员变量的同一个值。
     * 一个 volatile 对象引用可能是 null。
     */
    private volatile boolean testBool = false;
    public void methodOne() {
        testBool = true;
        while (testBool) {
            System.out.println("Test");
        }
    }
    public void methodTwo() {
        testBool = false;
        while (testBool) {}
    }
}

 

补充说明:

abstract不能和其他任何非访问修饰符同时使用;

volatile和final不能同时使用;

synchronized和strictfp不能同时使用;

abstract 和 native 修饰的方法没有方法体;

当一个类包含abstract方法时,此类必须定义为abstract;

 

参考资料:

http://www.runoob.com/java/java-modifier-types.html

 

 

 本文作者: sylan215

 本文地址: http://www.sylan215.com/

 版权声明: 本文首发于公众号「sylan215」,可以随意转载,但必须在明确位置注明出处!

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值