Why am I able to call private method?

后端 未结 8 1466
深忆病人
深忆病人 2020-12-10 21:29

I should not be able to invoke a private method of an instantiated object. I wonder why the code below works.

public class SimpleApp2 {
    /**
     * @param         


        
相关标签:
8条回答
  • 2020-12-10 21:53

    From the Java Tutorial:

    private modifier—the field is accessible only within its own class

    The main method is inside the same class as the private method and thus has access to it.

    0 讨论(0)
  • 2020-12-10 22:04

    Because the private scope limits access to the class defining the method, and your main happens to be in the same class.

    private modifier—the field is accessible only within its own class.

    See Access Modifiers in the Java Documentation.

    0 讨论(0)
提交回复
热议问题