Java 中字段和变量的的区别(Fields vs Variables in Java)

来源:互联网 发布:网络监控机怎么安装 编辑:程序博客网 时间:2024/06/05 23:41

转载至:http://edayan.info/java/fields-vs-variables-in-java

Fields vs Variables in Java

  • Fields vs Variables in Java
    • What is a field
    • Different types of variables
      • Class variables
      • Instance variables
      • Local variables
      • Parameter Variables
    • Difference between variable and field

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.
    Public static final String DEPARTMENT_CODE = "departmentCode";
  • If static variables are not constant they are defined in the same way as instance variables. For example
    public static double basicSalary;

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
    private String employeeName;

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.
     private void printMyName() {          String myName = "java"; //myName is a local variable.          System.out.println(myName);        }

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.
     private void printMyName(String myName/*parameter variable*/) {          int myId = 1234;//local variable          System.out.println(myName);          System.out.println(myId);        }

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.