mazem
mazem

Reputation: 1

can we access public method defined in a default class outside the package in java?

The main method in java is defined as a public method, and this method is defined in a default class. lets say

class test{
    public static void main(String args[]){
        System.out.println("Hi");
    }
}

can you please explain how the JVM is able to access this main method as the class is default and it can be accessed only with in the package.

Upvotes: 0

Views: 816

Answers (1)

dlev
dlev

Reputation: 48596

You're thinking of the JVM as a bunch of Java code in some other package, which therefore couldn't access the main method hidden in your class with default accessibility. But it's not. The JVM is the virtual machine on which Java code is run; it decides what is and is not accessible to other Java code. In particular, it can run whichever methods it likes, regardless of their accessibility.

Upvotes: 4

Related Questions