Back to Java
2025-12-225 min read

public

Learn public step by step with clear examples and exercises.

Title: Understanding the public Keyword in Java - A full guide

Why This Matters

The public keyword is a fundamental concept in Java programming, playing a crucial role in defining the accessibility and visibility of classes, methods, and variables. It's essential for any Java developer to understand its significance, especially when working on projects that involve collaboration or are meant to be used by others. In this tutorial, we will delve into the importance of public in Java, explore its usage, and discuss common mistakes to avoid.

Prerequisites

Before diving into the core concept, it's important to have a basic understanding of:

  1. Variables and data types in Java
  2. Basic syntax for declaring methods (functions) in Java
  3. Understanding access modifiers in Java

Core Concept

What is public?

In Java, the keyword public is an access modifier that determines the visibility of classes, methods, and variables at runtime. When a class or member (variable or method) is declared as public, it can be accessed from any other class within the same package or from outside the package, making it visible to the entire Java application.

Using public with Classes

To create a public class, simply add the keyword public before the class name:

public class MyClass {
// class body
}

By declaring a class as public, you make it accessible to other classes in your project. This is particularly important when creating reusable libraries or APIs, where the intention is for other developers to use and extend your code.

Using public with Methods

Similarly, to create a public method (function), add the keyword public before the return type:

public int myMethod() {
// method body
}

By declaring a method as public, you make it accessible to other classes in your project. This is useful when you want to expose functionality of one class to others, allowing them to call and use that functionality.

Using public with Variables

Declaring variables as public makes them accessible from any other class within the same package or from outside the package:

public int myVariable;

Note that that making a variable public can lead to potential issues related to data integrity and security, so it should be used judiciously.

Access Modifiers in Java

In addition to public, Java provides other access modifiers such as private, protected, and default. Each of these modifiers determines the visibility of classes, methods, and variables at different levels within your project:

  1. public: accessible from any class within the same package or from outside the package
  2. private: only accessible within the current class
  3. protected: accessible within the current class and subclasses of that class, as well as within the same package
  4. default (no modifier): accessible within the same package but not outside it

Worked Example

To better understand how to use the public keyword in Java, let's create a simple example:

// A public class that contains a public method
public class MyClass {
// A public variable
public int myVariable;

// A public method that returns the value of myVariable
public int getMyVariable() {
return myVariable;
}
}

// Another class in the same package that can access MyClass and its public members
public class Main {
public static void main(String[] args) {
// Create an instance of MyClass
MyClass myObj = new MyClass();

// Access the public variable and method of MyClass
myObj.myVariable = 42;
System.out.println("The value of myVariable is: " + myObj.getMyVariable());
}
}

In this example, we create a public class called MyClass that contains a public variable (myVariable) and a public method (getMyVariable()). We then create another class called Main, which is also in the same package as MyClass. Since both classes are public, they can access each other's public members. In the main method of Main, we create an instance of MyClass, set the value of myVariable, and call the getMyVariable() method to print its value.

Common Mistakes

  1. Forgetting the public keyword when declaring a class or method: If you forget to make your class or method public, it will only be accessible within the same package, which may cause issues if you want to use it in other packages or share it with others.
  2. Overusing public variables: Making too many variables public can lead to data integrity and security issues, as it makes the data more vulnerable to manipulation from outside sources. Use public variables sparingly and only when necessary.
  3. Incorrect use of access modifiers: Misunderstanding the differences between public, private, protected, and default can lead to unintended visibility issues within your codebase. Make sure you understand each access modifier's purpose and use them appropriately.
  4. Ignoring access modifiers when working with libraries or APIs: When using third-party libraries or APIs, it's important to pay attention to their access modifiers, as they may have different visibility requirements than your own code.

Practice Questions

  1. What is the purpose of the public keyword in Java?
  2. How can you make a class accessible from other classes within your project and outside the package?
  3. Can you explain the differences between private, protected, and default access modifiers in Java?
  4. Why should you be cautious when using public variables in your code?
  5. What are some common mistakes to avoid when working with the public keyword in Java?

FAQ

Question: Can I make a method private and still call it from another class within the same package?

Answer: No, if you declare a method as private, it can only be accessed within the current class. To allow other classes in the same package to access the method, use the public access modifier.

Question: Is it necessary to make all my classes public when working on a project?

Answer: Not necessarily. You should only declare a class as public if you intend for it to be used by other classes within your project or shared with others. If a class is not meant to be reused, there's no need to make it public.

Question: What happens if I forget to add the public keyword when declaring a method in an interface?

Answer: Methods in interfaces do not require the public keyword by default. However, if you explicitly declare a method as public, it will still be accessible from other classes that implement the interface.

public | Java | XQA Learn