Thursday, July 9, 2009

Java modifiers summary

ModifierClassInterfaceInner ClassInner InterfaceVariableMethodConstructorFree-Floating Block
publicyesyesyesyesyesyesyesno
protectednonoyesyesyesyesyesno
none or package or defaultyesyesyesyesyesyesyesyes
privatenonoyesyesyesyesyesno
finalyesnoyesnoyesyesnono
abstractyesyes/noyesyes/nonoyesnono
staticnonoyesyesyesyesnoyes
nativenononononoyesnono
transientnonononoyesnonono
volatilenonononoyesnonono
synchronizednononononoyesnoyes
strictfpyesyesyesyesnoyesyesno

Points:
  • all access modifiers can be used to classes and members except for protected and private which cannot be used to outer classes and interfaces

  • free-floating block cannot have access modifiers. it can only use static or synchronized

  • constructors can only use access modifiers and strictfp

  • native is only for methods

  • transient and volatile are only for variables. Transient indicates that it is not serializable. Volatile indicates that it can be modified simultaneously by many threads.

  • synchronized can only be used on methods or free-floating blocks

  • strictfp can't be used on variables or free-floating blocks

    Using strictfp ensures that you get the same result of floating-point expressions across multiple platforms. But it may also result to overflow or underflow hence the expression, "Write-Once-Get-Equally-Wrong-Results-Everywhere". If you don't use strictfp, JVM can calculate floating-point expressions however it want and thus could produce more accurate results.

2 comments: