Accessing Class Members in Java

Java classes consist of various variables and instance members (or methods). Before accessing class members in Java like variables and instance members, it is essential to declare them. So, let us first understand how to declare variables and methods.

Declaration of Instance Variables

All variables which are defined in a class are known as instance variables as each object of the class will have its own copy of the variables. Hence the data in various objects are unique and are separate from one another.

When declaring variables, there are three Access Level Modifiers. These are:

Modifier Access Level
Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
private Y N N N

The public modifier is the default modifier and the easiest access specifier. It is used to specify that the member is visible to all the classes and can be accessed from everywhere. We should declare public members only if such access does not produce undesirable results. These act like secrets which can be known by anybody.

The protected modifier specifies that the member can be accessed by its own members or the members of its sub-class only. These act as family secrets which only the concerned family knows but no outsiders know about it.

The private modifier is the most restrictive access level. It specifies that the members can only be accessed by its own class i.e. the class where it is defined. This is used with those variables or methods that contain information that need not be accessed by an outsider as it could make the program inconsistent. This modifier is the main way by which an object encapsulates itself and hides its data from the outside world. Hence, these act as secrets which we do not tell anybody.

The syntax for declaration is as follows:

Access_modifier data_type var_name;

For example: public int students;

Declaration of Methods:

A method is that part of the program that contains the code regarding the action to be taken or the task to be carried out. A class can contain any number of functions depending on the functionality of that program and each function can be called any number of times during program execution. The syntax to declare a method is as follows:

Access_modifier data_type method_name ([parameter1, parameter2 …]) {

// Body of the method

}

To execute a method you can invoke or call it from other methods as and when needed.

Accessing Class Members in Java-Instance Variables and Methods using Objects

Instance variables and methods are accessed via objects with the help of a dot (.) operator. The dot operator creates a link between the name of the instance variable and the name of the class with which it is used.

For example if salary class had a variable named basicsal and we wanted to initialize it to 10000, then we would use the following code:

s.basicsal = 10000;

Where, s is the name of the object and basicsal is the instance variable.

Now let us take up an example.

 class Employee
{
      String name;
      int age;
      float bsal, gsal;

      public void acceptDetails(String n, int a, int s)
      {
            name = n;
            age = a;
            bsal = s;
      }
      public void showData()
      {
            System.out.println("Employee Name = " + name);
            System.out.println("Employee Age = " + age);
            System.out.println("Employee Basic Salary = " + bsal);
            System.out.println("Employee Gross Salary = " + gsal);
      }
      void cal()            // public by default
      {
            gsal = bsal - (bsal*5/100);
      }

      public static void main(String args [])
      {
            Employee e1 = new Employee();      // object e1 created
            e1.acceptDetails("Nancy", 28, 12000);
            e1.cal();
            e1.showData();
      }
}

Output:

Accessing Class Members Java