Why can I access a private variable from main method?

Well, main() is part of the containing class. In fact, main() is exactly like every other method, except that you can start the JVM and tell it to run the main() method of a class from the command line.


Classes can access the private instance variables of (other) objects of the same type.

The following is also possible

public class Foo {

    private int a;

    public void mutateOtherInstance(Foo otherFoo) {
        otherFoo.a = 1;
    }
}

You could argue if this is desirably or not, but it's just a rule of life that the JLS has specified this is legal.


The main method is in the class Ferrari and thus can access private variables, even if it's static.


Main is a part of you class, you have declared it inside your class :) What main is not is part of your object, it will not be any part of the objects you create from the class but it is still part of the class. This is correct for any static function as main is just a normal static function that the framework knows it should look for when the program is executed.