Object Oriented Scope

In object oriented programming, methods and variables have various scope. Scope means that the method or variable may or may not be directly accessable to other objects or classes. Classes that do not have instances may be accessable to the system.

Class Scope

Class variables and class methods are associated with a class. An instance of the class (object) is not required to use these variables or metnods. Class methodc cannot access instance variables or metnods, only class variables and methods.

Instance Scope

Instance variables and instance methods are associated with a specific object. They can access class variables and methods.

Private Scope

Private variables and private methods are only accessible to the object they are contained in.

Protected Scope

Protected variables and protected methods are accessible by the class they are in and inheriting classes (sub classes).

Public Scope

Public variables and public methods are accessible outside the object they are contained in. They are accessible to any other object.

Encapsulation

The process of providing a public interface to interact with the object while hiding other information inside the object.

Java Variables and Scope

There are three types of variable scope in Java.

  • Class variables - Applies to all instances of a class of objects.
  • Instance variables - Define an objectís attributes.
  • Local variables - Are defined and used inside methods or other statement blocks such as a catch block. After the block or method is exited, the variables no longer exist. They must be declared and given values before use.