Sitemap

Java Modifiers

2 min readApr 23, 2023

--

A Java modifier is used to control the visibility of an interface, class, method, or a variable. Modifiers in Java can be either access modifiers or non-access modifiers. Using access modifiers, you can control which classes, methods, or variables are visible. These keywords can be used to restrict access to classes and members, either from within the same package or from other packages.

Press enter or click to view image in full size
Photo by William Felker on Unsplash

Access Modifiers:

  • public:The code is accessible for all classes
  • protected:The code is accessible in the same package and subclasses.
  • default:The code is only accessible in the same package. This is used when you don’t specify a modifier.
  • private:The code is only accessible within the declared class

Get Bora Fazli’s stories in your inbox

Join Medium for free to get updates from this writer.

Press enter or click to view image in full size

Non-Access Modifiers:

  • final:Attributes and methods cannot be overridden/modified
  • static:Attributes and methods belongs to the class, rather than an object
  • abstract:Can only be used in an abstract class, and can only be used on methods.
  • transient:Attributes and methods are skipped when serializing the object containing them
  • synchronized:Methods can only be accessed by one thread at a time
  • volatile:The value of an attribute is not cached thread-locally, and is always read from the "main memory"

--

--