-5

How did bird able to call the method fly if it's private

public class Bird {
    private static void fly() { 
        System.out.println("Bird is flying"); 
    }
    public static void main(String[] args) {
        fly(); // make sense
        Bird bird = new Bird();
        bird.fly();
    }
}
John Joe
  • 12,412
  • 16
  • 70
  • 135
Java jansen
  • 187
  • 2
  • 15

2 Answers2

1

The access modifier private means that the method can only be seen from within the class. Since you are within the class, this is possible.

B.Jahrer
  • 11
  • 3
0

In Java there are 3 modifiers:

-Public: You can access from anywhere

-Private: You can access from the same class

-Protected You can access from the same package and from any class that extends the class in which it is.

Your method is private and you are accessing it in the same class.

campanoon
  • 41
  • 1
  • 9