What Are 'Instance Variables' in Java?

The Java language allows developers to create applications using Object-Oriented modeling. In an Object-Oriented application, tasks are carried out by a group of objects. These objects have behavior and properties that are defined within class declarations. When programmers write such class declarations, they can use instance variables to model data items that are accessible throughout the class. Each object of a class has its own copy of any instance variables in the class declaration.

  1. Java Variables

    • Java variables store data values or object references. The following sample variable stores a primitive type integer value:

      int myNum = 5;

      The following example code demonstrates creating a variable to store an object reference, using the Integer class:

      Integer myNumber = new Integer(5);

      This stores an object reference rather than a data value as in the first example. When Java encounters this code, it will create an object according to the Integer class declaration, returning a reference to the new object's location in memory. Following the assignment operation indicated by the equals sign, the "myNumber" variable name will point to this location.

    Classes

    • When programmers create applications in Java using Object-Oriented techniques, they create one or more class declarations, defining the objects they need to carry out the set of tasks within the application as a whole. Within a class declaration, developers can include variables and methods. These variables and methods will exist for every instance of the class created within the application. The constructor method is particularly important, as when Java programs create an object of a class, the constructor method executes.

    Object Instances

    • Once a class declaration exists within a Java application, programmers can create object instances of the class. Programs can automatically also create objects of classes provided as standard within the language, such as the Integer class. The following sample code demonstrates creating an object of the class "Helper" in an application with a "Helper" class declaration:

      Helper myHelp = new Helper("Jim");

      This would work in a case where the class constructor takes a string parameter.

    Instance Variables

    • Instance variables store data for single instances of a class. For example, in the "Helper" class, the following instance variable could be declared:

      private String myName;

      This would normally appear before the constructor method, which can instantiate it as follows:

      public Helper(String helperName) {

      myName = helperName;

      }

      It is common practice to ensure all instance variables have been assigned a value by the time the constructor method finishes executing. Instance variables are normally declared with private visibility, which means that their values can only be changed within the class.

    Access

    • Code external to a class can sometimes access the instance variables through class methods as in the following example code:

      public String getName(){

      return myName;

      }

      External code can access the variable by calling this method on an instance of the class, as follows:

      String theName = myHelp.getName();

      This is an "accessor" method. Any methods providing external code with the ability to modify the values of instance variables, "mutator" methods, can carry out checks to prevent invalid values from being used.

Related Searches:

References

Resources

Comments

Related Ads

Featured