Summary table of Java Modifiers and Access Specifiers.

Summary table of Java Modifiers and Access Specifiers.

Summary table of Java Modifiers and Access Specifiers.

Summary table of Java Modifiers and Access Specifiers.
  • The only applicable modifier for the local variable is final.
  • The only applicable modifiers for constructor are public, private, default, protected.
  • The modifiers which are applicable only to the method is native.
  • The modifiers which are applicable only for variables volatile and transient.
  • The modifiers which are applicable for classes but not for an interface is final.
  • The modifier which is applicable for classes but not for enum is final, abstract.

 

Modifiers

Outer class

Inner class

Method

Variables

Blocks

Outer interface

Inner interface

Outer enum

Inner enum

Constructor

Public

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Private

Yes

Yes

Yes

Yes

Yes

Yes

Protected

Yes

Yes

Yes

Yes

Yes

Yes

Default

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Yes

final

Yes

Yes

Yes

Yes

abstract

Yes

Yes

Yes

Yes

Yes

static

Yes

Yes

Yes

Yes

Yes

Yes

synchronized

Yes

Yes

Native

Yes

strictfp

Yes

Yes

Yes

Yes

Yes

Yes

Yes

Transient

Yes

Volatile

Yes

 

Tips on Choosing an Access Level

  • Write your code like If other programmers use your class, you want to ensure that misuse cannot happen. Access levels can help you do this.
  • Always Use the most restrictive access modifiers that makes sense for a particular member. Use private unless you have a good reason not to go public.
  • Avoid public modifier except for constants. Public fields manager to link you to a particular implementation and limit your flexibility to change your code because if you change then other will get affected.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

*