您现在的位置是:首页 > 文章详情

java修饰符使用指南

日期:2018-05-27点击:317

java修饰符使用指南

1、什么是修饰符?


 指的是一种标识类型以及类型成员的访问范围的声明。 应用在修饰类名,类成员,方法,参数,构造器中。 

2、修饰符的有几种?


 一共大致有14种,分别为public、privateprotected、static、final、 synchronized、volatile、transient、native、interface、abstract、 strictfp、enumannotation 

对于这些,我们有些可能很熟悉,有些可能很陌生。总之,一半一半吧。我们先从源码分析。


3、java源码


 package java.lang.reflect; import java.security.AccessController; import sun.reflect.LangReflectAccess; import sun.reflect.ReflectionFactory; /** * The Modifier class provides {@code static} methods and * constants to decode class and member access modifiers. The sets * of modifiers are represented as integers with distinct bit * positions representing different modifiers. The values for the * constants representing the modifiers are taken from the tables * in sections 4.1, 4.4, 4.5, and 4.7 of <cite>The Java&trade; * Virtual Machine Specification</cite>. * * @see Class#getModifiers() * @see Member#getModifiers() * * @author Nakul Saraiya * @author Kenneth Russell */ public class Modifier { /* * Bootstrapping protocol between java.lang and * java.lang.reflect * packages */ static { sun.reflect.ReflectionFactory factory = AccessController.doPrivileged( new ReflectionFactory.GetReflectionFactoryAction()); factory.setLangReflectAccess( new java.lang.reflect.ReflectAccess()); } /** * Return {@code true} if the integer argument includes the * {@code public} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code public} modifier; {@code false} otherwise. */ public static boolean isPublic(int mod) { return (mod & PUBLIC) != 0; } /** * Return {@code true} if the integer argument includes the * {@code private} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code private} modifier; {@code false} otherwise. */ public static boolean isPrivate(int mod) { return (mod & PRIVATE) != 0; } /** * Return {@code true} if the integer argument includes the * {@code protected} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code protected} modifier; {@code false} otherwise. */ public static boolean isProtected(int mod) { return (mod & PROTECTED) != 0; } /** * Return {@code true} if the integer argument includes the * {@code static} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code static} modifier; {@code false} otherwise. */ public static boolean isStatic(int mod) { return (mod & STATIC) != 0; } /** * Return {@code true} if the integer argument includes the * {@code final} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code final} modifier; {@code false} otherwise. */ public static boolean isFinal(int mod) { return (mod & FINAL) != 0; } /** * Return {@code true} if the integer argument includes the * {@code synchronized} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code synchronized} modifier; {@code false} otherwise. */ public static boolean isSynchronized(int mod) { return (mod & SYNCHRONIZED) != 0; } /** * Return {@code true} if the integer argument includes the * {@code volatile} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code volatile} modifier; {@code false} otherwise. */ public static boolean isVolatile(int mod) { return (mod & VOLATILE) != 0; } /** * Return {@code true} if the integer argument includes the * {@code transient} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code transient} modifier; {@code false} otherwise. */ public static boolean isTransient(int mod) { return (mod & TRANSIENT) != 0; } /** * Return {@code true} if the integer argument includes the * {@code native} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code native} modifier; {@code false} otherwise. */ public static boolean isNative(int mod) { return (mod & NATIVE) != 0; } /** * Return {@code true} if the integer argument includes the * {@code interface} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code interface} modifier; {@code false} otherwise. */ public static boolean isInterface(int mod) { return (mod & INTERFACE) != 0; } /** * Return {@code true} if the integer argument includes the * {@code abstract} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code abstract} modifier; {@code false} otherwise. */ public static boolean isAbstract(int mod) { return (mod & ABSTRACT) != 0; } /** * Return {@code true} if the integer argument includes the * {@code strictfp} modifier, {@code false} otherwise. * * @param mod a set of modifiers * @return {@code true} if {@code mod} includes the * {@code strictfp} modifier; {@code false} otherwise. */ public static boolean isStrict(int mod) { return (mod & STRICT) != 0; } /** * Return a string describing the access modifier flags in * the specified modifier. For example: * <blockquote><pre> * public final synchronized strictfp * </pre></blockquote> * The modifier names are returned in an order consistent with * the suggested modifier orderings given in sections 8.1.1, * 8.3.1, 8.4.3, 8.8.3, and 9.1.1 of * <cite>The Java&trade; Language Specification</cite>. * The full modifier ordering used by this method is: * <blockquote> {@code * public protected private abstract static final transient * volatile synchronized native strictfp * interface } </blockquote> * The {@code interface} modifier discussed in this class is * not a true modifier in the Java language and it appears after * all other modifiers listed by this method. This method may * return a string of modifiers that are not valid modifiers of a * Java entity; in other words, no checking is done on the * possible validity of the combination of modifiers represented * by the input. * * Note that to perform such checking for a known kind of entity, * such as a constructor or method, first AND the argument of * {@code toString} with the appropriate mask from a method like * {@link #constructorModifiers} or {@link #methodModifiers}. * * @param mod a set of modifiers * @return a string representation of the set of modifiers * represented by {@code mod} */ public static String toString(int mod) { StringBuilder sb = new StringBuilder(); int len; if ((mod & PUBLIC) != 0) sb.append("public "); if ((mod & PROTECTED) != 0) sb.append("protected "); if ((mod & PRIVATE) != 0) sb.append("private "); /* Canonical order */ if ((mod & ABSTRACT) != 0) sb.append("abstract "); if ((mod & STATIC) != 0) sb.append("static "); if ((mod & FINAL) != 0) sb.append("final "); if ((mod & TRANSIENT) != 0) sb.append("transient "); if ((mod & VOLATILE) != 0) sb.append("volatile "); if ((mod & SYNCHRONIZED) != 0) sb.append("synchronized "); if ((mod & NATIVE) != 0) sb.append("native "); if ((mod & STRICT) != 0) sb.append("strictfp "); if ((mod & INTERFACE) != 0) sb.append("interface "); if ((len = sb.length()) > 0) /* trim trailing space */ return sb.toString().substring(0, len-1); return ""; } /* * Access modifier flag constants from tables 4.1, 4.4, 4.5, and * 4.7 of <cite>The Java&trade; Virtual Machine * Specification</cite> */ /** * The {@code int} value representing the {@code public} * modifier. */ public static final int PUBLIC = 0x00000001; /** * The {@code int} value representing the {@code private} * modifier. */ public static final int PRIVATE = 0x00000002; /** * The {@code int} value representing the {@code protected} * modifier. */ public static final int PROTECTED = 0x00000004; /** * The {@code int} value representing the {@code static} * modifier. */ public static final int STATIC = 0x00000008; /** * The {@code int} value representing the {@code final} * modifier. */ public static final int FINAL = 0x00000010; /** * The {@code int} value representing the {@code synchronized} * modifier. */ public static final int SYNCHRONIZED = 0x00000020; /** * The {@code int} value representing the {@code volatile} * modifier. */ public static final int VOLATILE = 0x00000040; /** * The {@code int} value representing the {@code transient} * modifier. */ public static final int TRANSIENT = 0x00000080; /** * The {@code int} value representing the {@code native} * modifier. */ public static final int NATIVE = 0x00000100; /** * The {@code int} value representing the {@code interface} * modifier. */ public static final int INTERFACE = 0x00000200; /** * The {@code int} value representing the {@code abstract} * modifier. */ public static final int ABSTRACT = 0x00000400; /** * The {@code int} value representing the {@code strictfp} * modifier. */ public static final int STRICT = 0x00000800; // Bits not (yet) exposed in the public API either because they // have different meanings for fields and methods and there is no // way to distinguish between the two in this class, or because // they are not Java programming language keywords static final int BRIDGE = 0x00000040; static final int VARARGS = 0x00000080; static final int SYNTHETIC = 0x00001000; static final int ANNOTATION = 0x00002000; static final int ENUM = 0x00004000; static final int MANDATED = 0x00008000; static boolean isSynthetic(int mod) { return (mod & SYNTHETIC) != 0; } static boolean isMandated(int mod) { return (mod & MANDATED) != 0; } // Note on the FOO_MODIFIERS fields and fooModifiers() methods: // the sets of modifiers are not guaranteed to be constants // across time and Java SE releases. Therefore, it would not be // appropriate to expose an external interface to this information // that would allow the values to be treated as Java-level // constants since the values could be constant folded and updates // to the sets of modifiers missed. Thus, the fooModifiers() // methods return an unchanging values for a given release, but a // value that can potentially change over time. /** * The Java source modifiers that can be applied to a class. * @jls 8.1.1 Class Modifiers */ private static final int CLASS_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | Modifier.STRICT; /** * The Java source modifiers that can be applied to an interface. * @jls 9.1.1 Interface Modifiers */ private static final int INTERFACE_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT; /** * The Java source modifiers that can be applied to a constructor. * @jls 8.8.3 Constructor Modifiers */ private static final int CONSTRUCTOR_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE; /** * The Java source modifiers that can be applied to a method. * @jls8.4.3 Method Modifiers */ private static final int METHOD_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT; /** * The Java source modifiers that can be applied to a field. * @jls 8.3.1 Field Modifiers */ private static final int FIELD_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT | Modifier.VOLATILE; /** * The Java source modifiers that can be applied to a method or * constructor parameter. * @jls 8.4.1 Formal Parameters */ private static final int PARAMETER_MODIFIERS = Modifier.FINAL; /** * */ static final int ACCESS_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE; /** * Return an {@code int} value OR-ing together the source language * modifiers that can be applied to a class. * @return an {@code int} value OR-ing together the source language * modifiers that can be applied to a class. * * @jls 8.1.1 Class Modifiers * @since 1.7 */ public static int classModifiers() { return CLASS_MODIFIERS; } /** * Return an {@code int} value OR-ing together the source * language modifiers that can be applied to an interface. * @return an {@code int} value OR-ing together the source * language modifiers that can be applied to an interface. * * @jls 9.1.1 Interface Modifiers * @since 1.7 */ public static int interfaceModifiers() { return INTERFACE_MODIFIERS; } /** * Return an {@code int} value OR-ing together the source * language modifiers that can be applied to a constructor. * @return an {@code int} value OR-ing together the source * language modifiers that can be applied to a constructor. * * @jls 8.8.3 Constructor Modifiers * @since 1.7 */ public static int constructorModifiers() { return CONSTRUCTOR_MODIFIERS; } /** * Return an {@code int} value OR-ing together the source * language modifiers that can be applied to a method. @return * an {@code int} value OR-ing together the source language * modifiers that can be applied to a method. * * @jls 8.4.3 Method Modifiers * @since 1.7 */ public static int methodModifiers() { return METHOD_MODIFIERS; } /** * Return an {@code int} value OR-ing together the source * language modifiers that can be applied to a field.@return * an {@code int} value OR-ing together the source language * modifiers that can be applied to a field. * * @jls 8.3.1 Field Modifiers * @since 1.7 */ public static int fieldModifiers() { return FIELD_MODIFIERS; } /** * Return an {@code int} value OR-ing together the source * language modifiers that can be applied to a parameter. * @return an {@code int} value OR-ing together the source * language modifiers that can be applied to a parameter. * * @jls 8.4.1 Formal Parameters * @since 1.8 */ public static int parameterModifiers() { return PARAMETER_MODIFIERS; } } 

4. 分析


具体的先不看,我们先看这里:

 /** * The Java source modifiers that can be applied to a class. * @jls 8.1.1 Class Modifiers */ private static final int CLASS_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | Modifier.STRICT; /** * The Java source modifiers that can be applied to an interface. * @jls 9.1.1 Interface Modifiers */ private static final int INTERFACE_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.ABSTRACT | Modifier.STATIC | Modifier.STRICT; /** * The Java source modifiers that can be applied to a constructor. * @jls 8.8.3 Constructor Modifiers */ private static final int CONSTRUCTOR_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE; /** * The Java source modifiers that can be applied to a method. * @jls8.4.3 Method Modifiers */ private static final int METHOD_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL | Modifier.SYNCHRONIZED | Modifier.NATIVE | Modifier.STRICT; /** * The Java source modifiers that can be applied to a field. * @jls 8.3.1 Field Modifiers */ private static final int FIELD_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE | Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT | Modifier.VOLATILE; /** * The Java source modifiers that can be applied to a method or * constructor parameter. * @jls 8.4.1 Formal Parameters */ private static final int PARAMETER_MODIFIERS = Modifier.FINAL; /** * */ static final int ACCESS_MODIFIERS = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE; 

4.1 分析:


 CLASS_MODIFIERS :表示的是类的修饰符。 INTERFACE_MODIFIERS:接口修饰符 CONSTRUCTOR_MODIFIERS:构造器修饰符 METHOD_MODIFIERS:方法修饰符 FIELD_MODIFIERS:字段修饰符 PARAMETER_MODIFIERS:参数修饰符 ACCESS_MODIFIERS:最基本的修饰符 

4.2 作用在类上:


 public:当此修饰符修饰类。那么,这个类将对外保持公开。也就是说,对任何包下的 任何类都是可用的。 

 private:当此修饰符修饰类。那么,这个类将对外不公开。也就是说,除类型创建者和 类型内部方法之外的任何元素都不能访问。 

 protected:当此修饰符修饰类。那么,这个类将对外保持半公开。可以理解为:同包、 子类和本身可以访问。当然,这里要注意一下,不同包下的子类不能访问。 

 abstract:当此修饰符修饰类。那么,这个类将表示抽象。抽象类表示的是一种默认行为。 在类里面可以定义的东西,抽象类也一样也可定义。同时,也可以定义默认方法,此方法 可以实现,也可以是类似于接口的抽象方法。 

 static:当此修饰符修饰类。那么这个类,只有一种情况,这个类是静态内部类。俗称: 内嵌类。只能访问静态的成员变量和方法,不能访问非静态的方法和属性,但是普通内部 类可以访问任意外部类的成员变量和方法 

 final: 当此修饰符修饰类。那么,就表明此类不希望从这个类继承。换言之,final 修饰的类不能被继承,也就是不能被扩展。不希望被子类化(子类处理)。 

 strictfp: 当此修饰符修饰类。那么,就声明此类所有运算都是精确的,而此类中的所有 方法也都是strictfp的。主要是用来精确浮点。 

4.3 作用在接口上:


 public:当此修饰符修饰接口。那么,这个接口将对外保持公开。也就是说,对任何包下的 任何类都是可实现的。 

 private:理论上,private是可以修饰接口的。实际上,接口是需要其他类实现的,如果 接口定义成private,而接口又要求被实现,但同时自己又不可见,这是无法实现的。假如定 义一个空接口,比如说Cloneable、Serializable接口,如果定义成private.没错,这是一 个空接口,同时又不见。那么,请问这个接口定义了和没定义有什么区别。所以,private不 能修饰接口。 

 protected:当此修饰符修饰接口。如果private不能修饰接口,那么这个应该可以了吧。 假设有一个protected接口A,那么只能位于同包下的类实现这个接口。于是同包下的类B就实 现这个接口A。这样是没错的。如果不同包下的类C去使用类b,可以使用吗?如果再如果不同 包下的类D继承类B怎么办。这样就失去了接口的重要意义:提供统一的接口,面向接口编程思 想也无法体现。所以,protected也不能修饰接口; 

 abstract:当此修饰符修饰接口。就表示为父接口。打个比方,如果目前一个功能,但 这个功能目前要有打印和显示的效果,随时可以扩展功能,那么这个时候,先定义一个父接口, 然后让子接口去继承它,如果功能需要扩展,我们就可以在更改接口的前提下,扩展功能。 

 static:当此修饰符修饰接口。那么这个类,只有一种情况,这个类是静态内部接口。俗 称:内嵌接口。可以理解为一个类或接口中进行进一步的逻辑细分, 比如JDK接口Map中的内 部接口Entry;可以增强代码的易读性和可维护性。 

 final: 这个修饰符理论上是可以修饰接口的,但实际上,它不能用来修饰接口, 因为 final修饰类,类不可以被继承,修饰接口,那么其它类不能实现,接口也就毫无意义了。 

 strictfp: 当此修饰符修饰接口。那么,就声明实现此接口的类所有运算都是精确的,而 实现类中的所有方法也都是strictfp的。主要是用来精确浮点。 

4.4 作用在构造器上:


在构造器上,只允许使用三种修饰符,private、protected、public。当然,还有一种,就是
什么也不写,表示默认的,对于在这个。先放在后面讲。


 public:当此修饰符修饰构造器。那么,这个构造器将对外保持公开。也就是说,对任何 包下的任何类都是可用的。 

 private:当此修饰符修饰构造器。那么,这个构造器将对外不公开。也就是说,除类型 创建者和类型内部方法之外的任何元素都不能访问。 

 protected:当此修饰符修饰构造器。那么,这个构造器将对外保持半公开。可以理解为: 同包、子类和本身可以访问。当然,这里要注意一下,不同包下的子类不能访问。 

4.5 作用于方法上的修饰符


 public:当此修饰符修饰方法。那么,这个方法将对外保持公开。也就是说,对任何 包下的任何类都是可用的。 

 private:当此修饰符修饰方法。那么,这个方法将对外不公开。也就是说,除类型 创建者和类型内部方法之外的任何元素都不能访问。 

 protected:当此修饰符修饰方法。那么,这个方法将对外保持半公开。可以理解为: 同包、子类和本身可以访问。当然,这里要注意一下,不同包下的子类不能访问。

---

 abstract:当此修饰符修饰方法,表示的是一种默认行为。 

 static:当此修饰符修饰方法,表示为静态方法,会随着类的定义而被分配和装载到 内存中。 

 final:当此修饰符修饰方法。那么,这个类一定是final类,这样可以把方法锁定, 防止任何继承类修改它的意义和实现。高效。编译器在遇到调用final方法时候会转入内嵌 机制,大大提高执行效率。 

 synchronized:当此修饰符修饰方法,那么,当一个线程使用到了被synchronized 修饰的方法,那么其他线程都必须等待,直至这个线程释放了锁,其他的线程才可以使用。 

 native:当此修饰符修饰方法,那么这个方法就是一个java调用非java代码的接口。 此方法的实现由非java语言实现,比如说CC++等。这个待征并非java所特有,很多其它 的编程语言都有这一机制。 

 strictfp:当此修饰符修饰方法。那么,在此方法内所有运算都是精确的,主要是用来 精确浮点。 

4.6 作用在字段上


 public:当此修饰符修饰字段。那么,这个字段将对外保持公开。也就是说,对任何 包下的任何类都是可用的。 

 private:当此修饰符修饰字段。那么,这个字段将对外不公开。也就是说,除类型 创建者和类型内部方法之外的任何元素都不能访问。 

 protected:当此修饰符修饰字段。那么,这个字段将对外保持半公开。可以理解为: 同包、子类和本身可以访问。当然,这里要注意一下,不同包下的子类不能访问。 

 static:当此修饰符修饰字段。那么,这个字段可以在没有创建对象的情况下进来访问 只要类被加载了,就可以通过类名去进行访问。 

 final:当此修饰符修饰字段。那么,这个字段一旦赋值,这个字段的值就无法改变。 不能赋值。一般在程序中多个地方使用到共同的数据,且该数据不会改变,此时我们专门定 义全局的常量。 

 transient:当此修饰符修饰字段。标记为transient的变量,在对象存储时,这些变量 状态不会被持久化。当对象序列化的保存在存储器上时,不希望有些字段数据被保存,为了保证 安全性,可以把这些字段声明为transien 

 volatile:当此修饰符修饰字段。在每次线程访问时,都强迫从共享内存中重读该成员 变量值。而且,当成员变量发生变化时,强迫线程将变化值回写到共享内存中。 

4.7 作用在参数上


final作用在参数上,那么如果定义的是基本类型,那么在这个方法的内部,基本类型 的值不能改变。但如果定义的是引用类型的变量,那么引用类型变量的引用不能改变,但引用类 型变量的值可以改变。 

5. 总结


5.1 关于 private、protected、public以及default


- protected修饰符所修饰的类(这句话中指父类)属成员变量和方法,只可以被子类访问,而不管子类是不是和父类位于同一个包中。default修饰符所修饰的类属成员变量和方法,只可被同一个包中的其他类访问,而不管其他类是不是该类的子类。protected属于子类限制修饰符,而default属于包限制修饰符。

5.2 关于 final


5.2.1 final 参数


final来修饰方法参数的原因是防止方法参数在调用时被改变,主要是这个原因,但可能会有歧 义,需要注意的点: 1.final修饰的方法参数中,如果修饰的是基本类型,那么在这个方法的内部,基本类型 的值是不能够改变的. 2.final修饰的方法参数中,如果修饰的是引用类型的变量,引用类型变量所指的引用是 不能够改变的,但是引用类型变量的值是可以改变的。 

5.2.2 final 其它情况


 - 如果final修饰类,这个类不可以从这个类继承,或者不允许其他任何人采取这种操作 

 - 如果修饰变量,那么这个变量一旦被赋值,就不可以被改变。 

5.3 abstract 和 interface


1.理解抽象类:指的是将某一事物的特性抽象出来,单独拎出来,进行类型隐藏,对某一事物的 行为抽象描述,但这组行为却能够有任意个可能的具体实现方式,这就是这个抽象描述就是抽 象类。 

2.interface是一种特殊形式的abstract class 

3.abstract class表示的是一种继承关系,一个类只能使用一次继承关系。但一个类可以实现 多个interface.可能是java语言设计者对多重继承的一种折中考虑。 

4.如果有必要,请在abstract class中定义默认行为。 

5.abstract classinterface是Java语言中的两种定义抽象类的方式,它们之间有很大的相 似性。但是对于它们的选择却又往往反映出对于问题领域中的概念本质的理解、对于设计意图 的反映是否正确、合理,因为它们表现了概念间的不同的关系(虽然都能够实现需求的功能)。 这其实也是语言的一种的惯用法, 

5.5 static


 1.static修饰的静态方法会随着类的定义而被分配和装载入内存中,编译器只为整个类创建了一 个静态变量的副本,也就是只分配一个内存空间,虽然可能有多个实例,但这些实例共享该内 存,特别值得注意的是,任何一个对象对静态数据成员的修改,都会影响其它对象。 

 2.静态不能引用非静态这一特性,是由于静态的会随着类的定义而被分配和装载入内存中这一关键 点决定的;如果静态引用了非静态的,根本无法从内存中找到非静态的代码段,势必会出错,这 种做法是Java虚拟机决不允许的 

5.6 关于strictfp


 strictfp 的意思是FP-strict,也就是说精确浮点的意思。在Java虚拟机进行浮点运  算时,如果没有指定strictfp关键字时,Java的编译器以及运 行环境在对浮点运算的表达式 是采取一种近似于我行我素的行为来完成这些操作,以致于得到的结果往往无法令你满意。而 一旦使用了strictfp来声明一个 类、接口或者方法时,那么所声明的范围内Java的编译器以 及运行环境会完全依照浮点规范IEEE-754来执行。因此如果你想让你的浮点运算更加精确,而 且不会因为不同的硬件平台所执行的结果不一致的话,那就请用关键字strictfp。 

5.7 关于transient 和 volatile


 1.当使用transient修饰符时,就意思着这个变量不会被持久化,所以如果对象在序列化的保存 在存储器上时,为了安全性,可以把某些不希望被保存的数据用transient修饰。 

 2.volatile具有synchronized的可见性,但不具备原子性,也就是说,线程可以自动发生 volatile变量的最新值。只有同时满足如下两个条件才可以使用volatile变量: 1.对变量的写操作不信赖于当前值。 2.该变量没有包含在具有其他变量的不变式中 

 3.实际上,这些条件表明,可以被写入 volatile 变量的这些有效值独立于任何程序的状态, 包括变量的当前状态。但使用volatile变量的次要原因是其性能:某些情况下,volatile变量同 步机制的性能要优于锁。volatile 操作不会像锁一样造成阻塞,因此,在能够安全使用 volatile 的情况下,volatile 可以提供一些优于锁的可伸缩特性。如果读操作的次数要远远超 过写操作,与锁相比,volatile 变量通常能够减少同步的性能开销。 

 4.与锁相比,Volatile 变量是一种非常简单但同时又非常脆弱的同步机制,它在某些情况下 将提供优于锁的性能和伸缩性。如果严格遵循 volatile 的使用条件 —— 即变量真正独立于其他 变量和自己以前的值 —— 在某些情况下可以使用 volatile 代替 synchronized 来简化代码。 

5.8 关于synchronized


 1.synchronized 修饰方法时锁定的是调用该方法的对象。它并不能使调用该方法的多个对象 在执行顺序上互斥。 

5.9 关于内部类


 1.使用内部类最吸引人的原因是:每个内部类都能独立地继承一个(接口的)实现,所以无论外 围类是否已经继承了某个(接口的)实现,对于内部类都没有影响。 

 
 2.内部类使得多重继承的解决方案变得更加完整。 
原文链接:https://yq.aliyun.com/articles/596999
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章