🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Java Notes
Topic #68

Java Modifiers


Modifiers

By now, you are quite familiar with the public keyword that appears in almost all of our examples:

public class Main

The public keyword is an access modifier, meaning that it is used to set the access level for classes, attributes, methods and constructors.

We divide modifiers into two groups:

  • Access Modifiers - controls the access level
  • Non-Access Modifiers - do not control access level, but provides other functionality

Access Modifiers

For classes, you can use either public or default:

Modifier Description Try it
public The class is accessible by any other class
default The class is only accessible by classes in the same package. This is used when you don't specify a modifier. You will learn more about packages in the Packages chapter

For attributes, methods and constructors, you can use the one of the following:

Modifier Description Try it
public The code is accessible for all classes
private The code is only accessible within the declared class
default The code is only accessible in the same package. This is used when you don't specify a modifier. You will learn more about packages in the Packages chapter
protected The code is accessible in the same package and subclasses. You will learn more about subclasses and superclasses in the Inheritance chapter

Public vs. Private Example

In the example below, the class has one public attribute and one private attribute.

Think of it like real life:

  • public - a public park, everyone can enter
  • private - your house key, only you can use it

Example

class Person {
  public String name = "John";   // Public - accessible everywhere
  private int age = 30;          // Private - only accessible inside this class
}

public class Main {
  public static void main(String[] args) {
    Person p = new Person();
    System.out.println(p.name);   // Works fine
    System.out.println(p.age);    // Error: age has private access in Person
  }
}

Example explained

Here, name is declared as public, so it can be accessed from outside the Person class. But age is declared as private, so it can only be used inside the Person class.

Want to go beyond the notes?

Join CodingNow 2.0's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Java Modifiers – FAQs

Quick answers about learning Java Modifiers in Java.

This free note from CodingNow 2.0 explains Java Modifiers in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on CodingNow 2.0, including Java Modifiers, is 100% free with no signup required.
With focused practice, most students grasp Java Modifiers in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now