A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yangguangyulei 中级黑马   /  2016-9-1 22:44  /  557 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

final的注意事项?

3 个回复

倒序浏览
final被修饰的字符不能修改,final不能与static连用
回复 使用道具 举报
楼上别误导别人
final 修饰符可以与static 同时使用的;
见java源码如下
[AppleScript] 纯文本查看 复制代码
    /**
     * The "standard" error output stream. This stream is already
     * open and ready to accept output data.
     * <p>
     * Typically this stream corresponds to display output or another
     * output destination specified by the host environment or user. By
     * convention, this output stream is used to display error messages
     * or other information that should come to the immediate attention
     * of a user even if the principal output stream, the value of the
     * variable <code>out</code>, has been redirected to a file or other
     * destination that is typically not continuously monitored.
     */
public final static PrintStream err = null;

final 和static 同时使用
此处是用于声明一个全局的不可更改的常量;
让使用者可以通过类名.来调用,
并且避免使用者通过某些方式对其更改,导致程序的后续运行出错.
有时候有些常量在当前类中使用频率较高;
或者是其调用者的使用频率较高,用final static 声明一个常量,可以编写代码的效率;
如数学中的PI;
若是,将其使用一次,便输入一次,是十分麻烦的,
[AppleScript] 纯文本查看 复制代码
    
/**
     * The {@code double} value that is closer than any other to
     * <i>pi</i>, the ratio of the circumference of a circle to its
     * diameter.
     */
    public static final double PI = 3.14159265358979323846;
回复 使用道具 举报
final可以和static连用,public static final a =0;是定义全局静态变量a的,final修饰方法不能被重写,修饰类不能被继承
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马