Default access modifier in Java - Time & Space Complexity
Let's see how the default access modifier affects the time complexity of code access.
We want to know how the cost of accessing members changes with input size.
Analyze the time complexity of accessing a default access modifier member in a class.
class Box {
int width; // default access
int getWidth() {
return width;
}
}
public class Main {
public static void main(String[] args) {
Box box = new Box();
box.width = 10;
System.out.println(box.getWidth());
}
}
This code defines a class with a default access field and accesses it.
Look for repeated actions that affect time.
- Primary operation: Accessing the
widthfield and callinggetWidth(). - How many times: Once in this example, but could be many times if in a loop.
Accessing a field or method does not depend on input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 accesses if repeated 10 times |
| 100 | 100 accesses if repeated 100 times |
| 1000 | 1000 accesses if repeated 1000 times |
Pattern observation: Each access is constant time, so total time grows linearly with number of accesses.
Time Complexity: O(1)
This means accessing a default access field or method takes the same time no matter the input size.
[X] Wrong: "Accessing a default access field takes longer as the program grows."
[OK] Correct: Access time depends on the operation itself, not on how many other classes or files exist.
Understanding access modifiers helps you reason about code organization and performance clearly, a useful skill in many coding tasks.
What if the width field was declared private and accessed via a getter method? How would the time complexity change?
Practice
default access modifier in Java?Solution
Step 1: Understand default access modifier meaning
The default access modifier means no keyword is used before a class, method, or variable, and it restricts access to the same package only.Step 2: Compare options with definition
It allows access only within the same package without using any keyword. correctly states it allows access only within the same package and requires no keyword. Other options mention keywords or access outside the package, which is incorrect.Final Answer:
It allows access only within the same package without using any keyword. -> Option CQuick Check:
Default access = package-only access [OK]
- Thinking default requires a keyword
- Confusing default with public or private
- Assuming default allows access from other packages
Solution
Step 1: Recall syntax for default access modifier
Default access means no access keyword is written before the class name.Step 2: Check each option
class MyClass {} declares the class without any access keyword, which means default access. Options A and B use explicit keywords, and C uses a non-existent keyword.Final Answer:
class MyClass {} -> Option AQuick Check:
No keyword = default access [OK]
- Using 'default' keyword which does not exist in Java
- Confusing private with default
- Always using public by habit
class A {
void show() {
System.out.println("Hello from A");
}
}
class B {
public static void main(String[] args) {
A a = new A();
a.show();
}
}What will be the output when class B is run?
Solution
Step 1: Analyze access modifiers in the code
Class A and method show() have default access (no keyword). Both classes are in the same package, so access is allowed.Step 2: Predict program output
Since B can access A and its show() method, the program prints "Hello from A" without errors.Final Answer:
Hello from A -> Option BQuick Check:
Default access allows same package access [OK]
- Assuming default means private access
- Thinking class A must be public to be used
- Confusing compile-time and runtime errors
pkg1 and pkg2, and these classes:package pkg1;
class C {
void display() {
System.out.println("Inside C");
}
}
package pkg2;
import pkg1.C;
public class D {
public static void main(String[] args) {
C c = new C();
c.display();
}
}What is the error in this code?
Solution
Step 1: Check class C's access modifier and package
Class C has default access (no keyword) and is in package pkg1, so it is accessible only within pkg1.Step 2: Check usage of C in pkg2
Class D in pkg2 tries to create an instance of C and call display(), but C is not visible outside pkg1, causing a compile-time error.Final Answer:
Cannot access class C from pkg2 because it has default access -> Option AQuick Check:
Default access = package-only visibility [OK]
- Assuming default access allows cross-package use
- Thinking display() is private
- Confusing import errors with access errors
tools with classes:class Helper {
void assist() {
System.out.println("Helping...");
}
}
public class Tool {
public static void main(String[] args) {
Helper h = new Helper();
h.assist();
}
}You want to allow only classes inside
tools to use Helper and its assist() method, but prevent access from outside. Which is the best way to achieve this?Solution
Step 1: Understand access goals
You want Helper and assist() accessible only inside the same package (tools) and hidden outside.Step 2: Match access modifiers to goals
Default access (no keyword) restricts access to the same package only, which fits the requirement. Public allows all access, private restricts to the class only, and protected allows subclasses and package access.Final Answer:
Keep Helper and assist() with default access (no keyword). -> Option DQuick Check:
Default access = package-only visibility [OK]
- Using public which exposes to all packages
- Using private which hides even inside package
- Using protected which allows subclass access outside package
Ready to prove it?
Pass the test and this pattern moves unknown
