0
0
Javaprogramming~15 mins

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

Choose your learning style8 modes available
flowchartConcept 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.
code_blocksExecution 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.
data_tableExecution 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.
search_insightsVariable Tracker
VariableStartAfter Step 3After Step 4Final
objnullMyClass instanceMyClass instanceMyClass instance
keyKey Moments - 3 Insights
Why can the Test class access MyClass and its display() method without any access modifier?
What happens if we try to access MyClass from a different package?
Is default access the same as public or private?
psychologyVisual 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
photo_cameraConcept 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.
contractFull 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.