Scopes of variables in Java

Java Programming Java8Java Technologies Object Oriented Programming

By default, a variable has default access. Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.

A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.

Java provides a number of access modifiers to set access levels for classes, variables, methods, and constructors. The four access levels are -

  • Visible to the package, the default. No modifiers are needed.

  • Visible to the class only (private).

  • Visible to the world (public).

  • Visible to the package and all subclasses (protected).

Published on 22-Jan-2018 10:07:16