The use of visibility modifiers in Java

joy picture joy · May 24, 2013 · Viewed 21.1k times · Source
class Orange{

    Orange(){
    }

}

What is the difference between the usage of the modifier - in this case, package-private - in front of the class and in front of the constructor? I think the modifier in front of the constructor means it is allowed to instantiate an instance of the class Orange. But what about the modifier in front of the class?

Answer

Rishikesh Dhokare picture Rishikesh Dhokare · May 24, 2013

To start with there are 4 access levels created by 3 access modifiers.

  1. public - accessible everywhere
  2. protected - accessible in the same package and in the children
  3. default - accessible only in the same package
  4. private - accessible only in the same class.

You are correct about - Modifiers at the level of constructors are directly related to the instantiation of the class.

Modifiers at the level of Class decide the accessibility of the Class.