Bird
Raised Fist0
Javaprogramming~10 mins

Default access modifier in Java - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Default access modifier
Declare class or member
↓
No access modifier specified?
Yes↓
Apply default (package-private) access
↓
Accessible within same package only
↓
Not accessible from other packages
When no access modifier is given, Java uses default access, allowing access only within the same package.
Execution Sample
Java
class MyClass {
    void display() {
        System.out.println("Hello");
    }
}

// Another class in same package
class Test {
    public static void main(String[] args) {
        MyClass obj = new MyClass();
        obj.display();
    }
}
This code shows a class and method with default access used within the same package.
Execution Table
StepActionAccess ModifierAccess Allowed?Output
1Declare class MyClass without modifierdefault (package-private)Accessible within package
2Declare method display() without modifierdefault (package-private)Accessible within package
3Create MyClass object in Test class (same package)N/AAllowed
4Call display() method on MyClass objectdefault (package-private)AllowedHello
5Try access from different package (not shown)default (package-private)Not allowedCompilation error
💡 Execution stops after successful method call; access outside package is blocked by default modifier.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
objnullMyClass instanceMyClass instanceMyClass instance
Key Moments - 3 Insights
Why can the Test class access MyClass and its display() method without any access modifier?
Because both classes are in the same package, the default access modifier allows access within the package as shown in execution_table rows 3 and 4.
What happens if we try to access MyClass from a different package?
Access is denied and causes a compilation error because default access restricts visibility to the same package only, as noted in execution_table row 5.
Is default access the same as public or private?
No, default access is package-private, meaning accessible only within the same package, unlike public (accessible everywhere) or private (accessible only within the class).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the display() method called successfully?
AStep 2
BStep 4
CStep 5
DStep 1
💡 Hint
Check the 'Action' and 'Output' columns in execution_table row 4.
According to the variable tracker, what is the value of 'obj' after Step 3?
Anull
Bundefined
CMyClass instance
Ddisplay() method
💡 Hint
Look at the 'After Step 3' column for 'obj' in variable_tracker.
If the MyClass was declared public instead of default, what would change in the execution table?
AAccess from other packages would be allowed
BAccess within the same package would be denied
Cdisplay() method would not be callable
DNo change at all
💡 Hint
Refer to execution_table row 5 about access from different packages.
Concept Snapshot
Default access modifier in Java means no modifier is written.
It allows access only within the same package.
Classes, methods, and variables without public/private/protected are package-private.
Access outside the package causes a compile error.
Use default for package-level encapsulation.
Full Transcript
In Java, when you declare a class or member without any access modifier, it uses the default access modifier, also called package-private. This means the class or member is accessible only within the same package. For example, if you have a class MyClass and a method display() both without modifiers, another class in the same package can create an object of MyClass and call display() successfully. However, if you try to access MyClass from a different package, the compiler will give an error because default access restricts visibility outside the package. This is different from public, which allows access everywhere, and private, which restricts access to within the class only. Understanding default access helps you control how your code is shared within your project packages.

Practice

(1/5)
1. Which statement best describes the default access modifier in Java?
easy
A. It restricts access only to the class itself.
B. It allows access from any package using the default keyword.
C. It allows access only within the same package without using any keyword.
D. It allows access only to subclasses in other packages.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    It allows access only within the same package without using any keyword. -> Option C
  4. Quick Check:

    Default access = package-only access [OK]
Hint: No keyword means default access is package-only [OK]
Common Mistakes:
  • Thinking default requires a keyword
  • Confusing default with public or private
  • Assuming default allows access from other packages
2. Which of the following is the correct way to declare a class with default access modifier?
easy
A. class MyClass {}
B. private class MyClass {}
C. default class MyClass {}
D. public class MyClass {}

Solution

  1. Step 1: Recall syntax for default access modifier

    Default access means no access keyword is written before the class name.
  2. 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.
  3. Final Answer:

    class MyClass {} -> Option A
  4. Quick Check:

    No keyword = default access [OK]
Hint: No keyword before class means default access [OK]
Common Mistakes:
  • Using 'default' keyword which does not exist in Java
  • Confusing private with default
  • Always using public by habit
3. Consider two classes in the same package:
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?
medium
A. Compile-time error: show() has private access
B. Hello from A
C. Compile-time error: class A is not public
D. Runtime error

Solution

  1. 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.
  2. Step 2: Predict program output

    Since B can access A and its show() method, the program prints "Hello from A" without errors.
  3. Final Answer:

    Hello from A -> Option B
  4. Quick Check:

    Default access allows same package access [OK]
Hint: Default access works within same package [OK]
Common Mistakes:
  • Assuming default means private access
  • Thinking class A must be public to be used
  • Confusing compile-time and runtime errors
4. Given two packages 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?
medium
A. Cannot access class C from pkg2 because it has default access
B. Cannot call display() because it is private
C. Cannot import package pkg1
D. No error, code runs fine

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Cannot access class C from pkg2 because it has default access -> Option A
  4. Quick Check:

    Default access = package-only visibility [OK]
Hint: Default access blocks cross-package access [OK]
Common Mistakes:
  • Assuming default access allows cross-package use
  • Thinking display() is private
  • Confusing import errors with access errors
5. You have a package 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?
hard
A. Make Helper and assist() public.
B. Use protected for Helper and assist().
C. Make Helper and assist() private.
D. Keep Helper and assist() with default access (no keyword).

Solution

  1. Step 1: Understand access goals

    You want Helper and assist() accessible only inside the same package (tools) and hidden outside.
  2. 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.
  3. Final Answer:

    Keep Helper and assist() with default access (no keyword). -> Option D
  4. Quick Check:

    Default access = package-only visibility [OK]
Hint: Default access hides from outside packages [OK]
Common Mistakes:
  • Using public which exposes to all packages
  • Using private which hides even inside package
  • Using protected which allows subclass access outside package
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard