Fields vs Variables in Java

What is a field in java? One of my friends asked me this question few days back. I know everyone knows the answer. I too did. Then he asked What is the difference between ‘variable’ and ‘field’ in Java? I didn’t expected that question at all. Actually that question never came to my mind before. Even if, then I would have guessed those are same. Then I searched for some time and found something interesting which I would like to share with you all.

Ok, Lets start from beginning. So before we go to the difference between field and variable we will analyse the particularities of variables and fields.

What is a field?

A ‘field’ is something which can store the state of an object. Objects keep their states by keeping values in these fields and different objects may have different values to fields. And also all fields are variables in java.

Different types of variables

There are 4 types of variables in java. Each of them are described below with their features.

  1. Class variables

    • They are static member variables in a class.
    • They are also called static variables.
    • They will be declared inside a class but outside of a method or constructor.
    • They can be accessed using class name like ClassName.variableName
    • They can have access modifiers private, protected, public or default
    • They share same copy across multiple instance. So they keep same value regardless of how much instance the class have.
    • Class variables are created when class is loaded to memory and destroyed when class is unloaded from memory
    • They are having highest scope as they can be accessed from any method or block in that class and from out side of the class with qualified name(ClassName.variableName).
    • If no value is assigned to them on declaration, then they will take default value based on their type. For example if class variable is a boolean, then it will take the value false by default.
    • By convention when a static variable is used as constant the variable name is written in upper case letters. The code snippet below is an example for this.
    • If static variables are not constant they are defined in the same way as instance variables. For example
  2. Instance variables

    • They are non static member variables in a class.
    • They can be accessed only with an object of class; like objectName.variableName
    • They will be declared inside a class but outside of a method or constructor.
    • They can have access modifiers private, protected, public or default
    • They have individual values in individual instances.
    • Instance variables are created when an object of the class is created using new keyword and they are destroyed when the object is destroyed.
    • They are having less scope than class variables and can be accessed/called directly using the variable name from within the class or from non static methods of the class
    • They can be accessed/called from other classes or from static methods using qualified name (like objectName.variableName)
    • Like class variables, they will take default values based on their type.
    • By convention they are used with private modifier and a public method will be provided to access these variables.
    • An example of instance variable declaration is given below
  3. Local variables

    • They are variables defined in methods, constructors or in loops.
    • They can be accessed using their name from within their scope.
    • They have scope inside that particular block only.
    • They will not have any access modifiers.
    • They are created when the block/method or constructor is accessed and destroyed when the block/method or constructor is exited.
    • They will not have default values. So local variables must be initialised before use.
    • The code snippet below is an example for a local variable inside a method.
  4. Parameter Variables

    • They are defined as arguments to methods, constructors etc.
    • The difference between Parameter variable and local variable is that Parameter variables are always defined at signature of method or constructor while local variables are defined inside method or constructor
      for example, consider the method below.

      Here myName is a parameter variable while myId is a local variable.
    • They can be accessed by their name from within their scope.
    • They have scope inside that particular method or constructor only.
    • They will not have any access modifiers.
    • They are created when the block/method or constructor is accessed and destroyed when the block/method or constructor is exited.

Difference between variable and field

Ok, now come back to our question.What is the difference between ‘variable’ and ‘field’ in Java. Actually these two terms are often used to represent same thing, but there are some exceptional situations. In the beginning of this article we have seen that a field can store the state of an object. Also we have seen that all fields are variables. So it is clear that there can be variables which are not fields. So looking into the 4 type of variables we can see that class variables and instance variables can affect the state of an object. In other words if a class or instance variable changes,the state of object changes. So we can say that class variables and instance variables are fields while local variables and parameter variables are not.

So that is all about variables in java.

Leave a Comment

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