Java member variable access constraints and scope

Member variable access limit

Java uses constraints on member variables to provide object-oriented features, encapsulation, and information hiding.

The member variable limiters provided by Java are public, private, and protected, and are available to the same package and subclass, unless they have separate access restrictions.

public

An object variable declared as public is always accessible when its class is accessible.

class Box {
  public int width;
  public int height;
  public int depth;
  public long idNum;
  static long boxID = 0;
  public Box() {
    idNum = boxID++;
  }
}

class PublicDemo {
  public static void main(String args[]) {
    Box mybox1 = new Box();
    Box mybox2 = new Box();
    mybox1.width = 7;
    mybox2.depth = 20;
  }
}

private

Object variables declared as private can be used only within the class to which they belong.

class Box {
  private int width;
  private int height;
  private int depth;
  ...
}

class PublicDemo {
  public static void main(String args[]) {
    Box mybox1 = new Box();
    mybox1.width = 7; //error
  }
}

protected

An object variable declared as protected can only be used in a class of the same package as a subclass of the class to which it belongs.

class Box {
  private int width;
  private int height;
  private int depth;
  protected int count; //Used in this class and its subclasses
  ...
}

class ProtectedDemo1 extends Box {
  //Create a subclass of the Box class using extends
  public static void main(String args[]) {
    Box mybox2 = new Box();
    mybox2.count = 7; //Accessible
  }
}

Effective range of variables

Variables can be separated by valid scope. Separating variables by their scope can be divided into member variables, method parameters and local variables, and exception handler parameters.

Leave a Reply

Your email address will not be published. Required fields are marked *

Latest Post

  • 8 things you need to think about for work-life balance
    View on Youtube Image: juliezhuo.com who works as a design VP for Facebook, presented 8 ways for work and life balance in her article “The
  • 10 special abilities of Smartphone
    View on Youtube No matter where you go or what you do, your smartphone is always with us. We use the internet with a smartphone,
  • Java: abstract class, abstract method
    An abstract class in Java is a class that declares an abstract function to be implemented in a subclass as an abstract method. An abstract
  • Java: inheritance and constructors
    When an object is created from an inherited class, the constructor of the superclass is performed before the constructor of the subclass. That is, when
  • Java reserved words: super, final
    super is a Java reserved word that is used to access member variables or methods of a superclass that is obscured by a subclass. Access
  • Java: finalize method and garbage collection
    The Java Virtual Machine(JVM) automatically performs garbage collection for objects that are no longer needed for efficient system operation. At this point, the JVM calls
  • Java: Method overloading
    Method overloading is the same concept as constructor overloading. That is, you can use methods of the same name in the same class, but methods
  • Java final variable
    You can specify an end variable using the reserved word final. The final variable represents a constant value that can not be changed. Typically, final
  • Java class variables
    Class variables are declared with static and have the concept of global variables. Class variables are used for the following purposes. Object references and object
  • Member variables in Java
    Java’s member variables are all declared outside the methods in the class and represent the properties that the object can have. Member variables consist of