What is the default scope of a method in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When discussing the scope of a method in Java, we're essentially exploring its accessibility from different parts of a program. In Java, methods are encapsulated within classes, and their accessibility is controlled via access modifiers. These modifiers determine whether other classes or packages can use the method. In Java, the default access level is often misunderstood, and understanding it can help in better designing your classes for security, reusability, and efficiency.
Java Access Modifiers
Java provides four access modifiers, and they determine the visibility of classes and class members like methods. Here's a quick rundown:
- public: The method is accessible from any other class.
- protected: The method is accessible within the same package or subclasses.
- default: Also known as "package-private"; the method is accessible only within its own package.
- private: The method is accessible only within the class it is declared.
Default Access Level
When you declare a method without specifying an access modifier, it takes on the default access level, often called "package-private." This means the method is accessible only within classes that are in the same package.
Example of Default Access Level
Consider two classes in the same package:
Now, consider accessing the display method from a class in a different package:
In this example, OtherMain cannot access display() because it resides in a different package from the Sample class.
Key Points
Below is a table summarizing the key points about Java method access levels:
| Access Modifier | Class | Package | Subclass | Global |
| public | Yes | Yes | Yes | Yes |
| protected | Yes | Yes | Yes | No |
| default | Yes | Yes | No | No |
| private | Yes | No | No | No |
- "Class" refers to the class people are working on.
- "Package" refers to other classes in the same package.
- "Subclass" refers to derived classes, possibly in different packages.
- "Global" refers to classes outside the package and subclass hierarchy.
Additional Details
Why Use Default Access?
Using default access is particularly beneficial in scenarios where:
- You want to limit the method's access to only those classes that logically form a coherent module.
- You aim to promote encapsulation by preventing access by unrelated classes.
- You wish to hide the internal implementation while exposing a simpler interface.
Alternatives to Default Scope
Depending on your design and architectural needs, you might consider using other access levels:
- public: When you want universally accessible methods.
- protected: Ideal if you plan to extend your class and want subclasses to reuse or modify behavior.
- private: Use this to encapsulate the behavior entirely within the class.
Conclusion
The default scope in Java plays a crucial role in maintaining the integrity and modularity of your applications. By constraining access to its containing package, it provides a balanced encapsulation level. As you design Java applications, understanding and using the default and other access levels can significantly impact your software's robustness and maintainability.
Related reading
- What is the difference between == and equals() in Java?
- What is the difference between ? and Object in Java generics?
- What is the difference between a HashMap and a TreeMap?
- What is the difference between a JavaBean and a POJO?
- What is the difference between a static and a non-static initialization code block
- What is the difference between an int and an Integer in Java and C#?
- What is the difference between and in Java?
- What is the difference between ArrayList.clear() and ArrayList.removeAll()?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.