指的是一種標識類型以及類型成員的訪問範圍的聲明。 應用在修飾類名,類成員,方法,參數,構造器中。
一共大體有14種,分別爲public、private、protected、static、final、 synchronized、volatile、transient、native、interface、abstract、 strictfp、enum、annotation。
對於這些,咱們有些可能很熟悉,有些可能很陌生。總之,一半一半吧。咱們先從源碼分析。html
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™ * 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™ 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™ 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; } }
具體的先不看,咱們先看這裏:java
/** * 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;
CLASS_MODIFIERS :表示的是類的修飾符。 INTERFACE_MODIFIERS:接口修飾符 CONSTRUCTOR_MODIFIERS:構造器修飾符 METHOD_MODIFIERS:方法修飾符 FIELD_MODIFIERS:字段修飾符 PARAMETER_MODIFIERS:參數修飾符 ACCESS_MODIFIERS:最基本的修飾符
public:當此修飾符修飾類。那麼,這個類將對外保持公開。也就是說,對任何包下的 任何類都是可用的。
private:當此修飾符修飾類。那麼,這個類將對外不公開。也就是說,除類型建立者和 類型內部方法以外的任何元素都不能訪問。
protected:當此修飾符修飾類。那麼,這個類將對外保持半公開。能夠理解爲:同包、 子類和自己能夠訪問。固然,這裏要注意一下,不一樣包下的子類不能訪問。
abstract:當此修飾符修飾類。那麼,這個類將表示抽象。抽象類表示的是一種默認行爲。 在類裏面能夠定義的東西,抽象類也同樣也可定義。同時,也能夠定義默認方法,此方法 能夠實現,也能夠是相似於接口的抽象方法。
static:當此修飾符修飾類。那麼這個類,只有一種狀況,這個類是靜態內部類。俗稱: 內嵌類。只能訪問靜態的成員變量和方法,不能訪問非靜態的方法和屬性,可是普通內部 類能夠訪問任意外部類的成員變量和方法
final: 當此修飾符修飾類。那麼,就代表此類不但願從這個類繼承。換言之,final 修飾的類不能被繼承,也就是不能被擴展。不但願被子類化(子類處理)。
strictfp: 當此修飾符修飾類。那麼,就聲明此類全部運算都是精確的,而此類中的全部 方法也都是strictfp的。主要是用來精確浮點。
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的。主要是用來精確浮點。
在構造器上,只容許使用三種修飾符,private、protected、public。固然,還有一種,就是
什麼也不寫,表示默認的,對於在這個。先放在後面講。編程
public:當此修飾符修飾構造器。那麼,這個構造器將對外保持公開。也就是說,對任何 包下的任何類都是可用的。
private:當此修飾符修飾構造器。那麼,這個構造器將對外不公開。也就是說,除類型 建立者和類型內部方法以外的任何元素都不能訪問。
protected:當此修飾符修飾構造器。那麼,這個構造器將對外保持半公開。能夠理解爲: 同包、子類和自己能夠訪問。固然,這裏要注意一下,不一樣包下的子類不能訪問。
public:當此修飾符修飾方法。那麼,這個方法將對外保持公開。也就是說,對任何 包下的任何類都是可用的。
private:當此修飾符修飾方法。那麼,這個方法將對外不公開。也就是說,除類型 建立者和類型內部方法以外的任何元素都不能訪問。
protected:當此修飾符修飾方法。那麼,這個方法將對外保持半公開。能夠理解爲: 同包、子類和自己能夠訪問。固然,這裏要注意一下,不一樣包下的子類不能訪問。
---安全
abstract:當此修飾符修飾方法,表示的是一種默認行爲。
static:當此修飾符修飾方法,表示爲靜態方法,會隨着類的定義而被分配和裝載到 內存中。
final:當此修飾符修飾方法。那麼,這個類必定是final類,這樣能夠把方法鎖定, 防止任何繼承類修改它的意義和實現。高效。編譯器在遇到調用final方法時候會轉入內嵌 機制,大大提升執行效率。
synchronized:當此修飾符修飾方法,那麼,當一個線程使用到了被synchronized 修飾的方法,那麼其餘線程都必須等待,直至這個線程釋放了鎖,其餘的線程纔可使用。
native:當此修飾符修飾方法,那麼這個方法就是一個java調用非java代碼的接口。 此方法的實現由非java語言實現,好比說C、C++等。這個待徵並不是java所特有,不少其它 的編程語言都有這一機制。
strictfp:當此修飾符修飾方法。那麼,在此方法內全部運算都是精確的,主要是用來 精確浮點。
public:當此修飾符修飾字段。那麼,這個字段將對外保持公開。也就是說,對任何 包下的任何類都是可用的。
private:當此修飾符修飾字段。那麼,這個字段將對外不公開。也就是說,除類型 建立者和類型內部方法以外的任何元素都不能訪問。
protected:當此修飾符修飾字段。那麼,這個字段將對外保持半公開。能夠理解爲: 同包、子類和自己能夠訪問。固然,這裏要注意一下,不一樣包下的子類不能訪問。
static:當此修飾符修飾字段。那麼,這個字段能夠在沒有建立對象的狀況下進來訪問 只要類被加載了,就能夠經過類名去進行訪問。
final:當此修飾符修飾字段。那麼,這個字段一旦賦值,這個字段的值就沒法改變。 不能賦值。通常在程序中多個地方使用到共同的數據,且該數據不會改變,此時咱們專門定 義全局的常量。
transient:當此修飾符修飾字段。標記爲transient的變量,在對象存儲時,這些變量 狀態不會被持久化。當對象序列化的保存在存儲器上時,不但願有些字段數據被保存,爲了保證 安全性,能夠把這些字段聲明爲transien
volatile:當此修飾符修飾字段。在每次線程訪問時,都強迫從共享內存中重讀該成員 變量值。並且,當成員變量發生變化時,強迫線程將變化值回寫到共享內存中。
當final做用在參數上,那麼若是定義的是基本類型,那麼在這個方法的內部,基本類型 的值不能改變。但若是定義的是引用類型的變量,那麼引用類型變量的引用不能改變,但引用類 型變量的值能夠改變。
- protected修飾符所修飾的類(這句話中指父類)屬成員變量和方法,只能夠被子類訪問,而無論子類是否是和父類位於同一個包中。default修飾符所修飾的類屬成員變量和方法,只可被同一個包中的其餘類訪問,而無論其餘類是否是該類的子類。protected屬於子類限制修飾符,而default屬於包限制修飾符。
final來修飾方法參數的緣由是防止方法參數在調用時被改變,主要是這個緣由,但可能會有歧 義,須要注意的點: 1. 在final修飾的方法參數中,若是修飾的是基本類型,那麼在這個方法的內部,基本類型 的值是不可以改變的. 2. 在final修飾的方法參數中,若是修飾的是引用類型的變量,引用類型變量所指的引用是 不可以改變的,可是引用類型變量的值是能夠改變的。
- 若是final修飾類,這個類不能夠從這個類繼承,或者不容許其餘任何人採起這種操做
- 若是修飾變量,那麼這個變量一旦被賦值,就不能夠被改變。
1.理解抽象類:指的是將某一事物的特性抽象出來,單獨拎出來,進行類型隱藏,對某一事物的 行爲抽象描述,但這組行爲卻可以有任意個可能的具體實現方式,這就是這個抽象描述就是抽 象類。
2.interface是一種特殊形式的abstract class
3.abstract class表示的是一種繼承關係,一個類只能使用一次繼承關係。但一個類能夠實現 多個interface.多是java語言設計者對多重繼承的一種折中考慮。
4.若是有必要,請在abstract class中定義默認行爲。
5.abstract class和interface是Java語言中的兩種定義抽象類的方式,它們之間有很大的相 似性。可是對於它們的選擇卻又每每反映出對於問題領域中的概念本質的理解、對於設計意圖 的反映是否正確、合理,由於它們表現了概念間的不一樣的關係(雖然都可以實現需求的功能)。 這其實也是語言的一種的慣用法,
1.static修飾的靜態方法會隨着類的定義而被分配和裝載入內存中,編譯器只爲整個類建立了一 個靜態變量的副本,也就是隻分配一個內存空間,雖然可能有多個實例,但這些實例共享該內 存,特別值得注意的是,任何一個對象對靜態數據成員的修改,都會影響其它對象。
2.靜態不能引用非靜態這一特性,是因爲靜態的會隨着類的定義而被分配和裝載入內存中這一關鍵 點決定的;若是靜態引用了非靜態的,根本沒法從內存中找到非靜態的代碼段,勢必會出錯,這 種作法是Java虛擬機決不容許的
strictfp 的意思是FP-strict,也就是說精確浮點的意思。在Java虛擬機進行浮點運 算時,若是沒有指定strictfp關鍵字時,Java的編譯器以及運 行環境在對浮點運算的表達式 是採起一種近似於我行我素的行爲來完成這些操做,以至於獲得的結果每每沒法令你滿意。而 一旦使用了strictfp來聲明一個 類、接口或者方法時,那麼所聲明的範圍內Java的編譯器以 及運行環境會徹底依照浮點規範IEEE-754來執行。所以若是你想讓你的浮點運算更加精確,而 且不會由於不一樣的硬件平臺所執行的結果不一致的話,那就請用關鍵字strictfp。
1.當使用transient修飾符時,就意思着這個變量不會被持久化,因此若是對象在序列化的保存 在存儲器上時,爲了安全性,能夠把某些不但願被保存的數據用transient修飾。
2.volatile具備synchronized的可見性,但不具有原子性,也就是說,線程能夠自動發生 volatile變量的最新值。只有同時知足以下兩個條件纔可使用volatile變量: 1.對變量的寫操做不信賴於當前值。 2.該變量沒有包含在具備其餘變量的不變式中
3.實際上,這些條件代表,能夠被寫入 volatile 變量的這些有效值獨立於任何程序的狀態, 包括變量的當前狀態。但使用volatile變量的次要緣由是其性能:某些狀況下,volatile變量同 步機制的性能要優於鎖。volatile 操做不會像鎖同樣形成阻塞,所以,在可以安全使用 volatile 的狀況下,volatile 能夠提供一些優於鎖的可伸縮特性。若是讀操做的次數要遠遠超 過寫操做,與鎖相比,volatile 變量一般可以減小同步的性能開銷。
4.與鎖相比,Volatile 變量是一種很是簡單但同時又很是脆弱的同步機制,它在某些狀況下 將提供優於鎖的性能和伸縮性。若是嚴格遵循 volatile 的使用條件 —— 即變量真正獨立於其餘 變量和本身之前的值 —— 在某些狀況下可使用 volatile 代替 synchronized 來簡化代碼。
1.synchronized 修飾方法時鎖定的是調用該方法的對象。它並不能使調用該方法的多個對象 在執行順序上互斥。
1.使用內部類最吸引人的緣由是:每一個內部類都能獨立地繼承一個(接口的)實現,因此不管外 圍類是否已經繼承了某個(接口的)實現,對於內部類都沒有影響。
2.內部類使得多重繼承的解決方案變得更加完整。
參考:
深刻理解abstract class和interface
正確使用 Volatile 變量
Java中static方法和普通方法的區別
Java語言中關鍵字strictfp的用途
JAVA方法中的參數用final來修飾的效果
詳解內部類
若是有侵權,立刻刪除app