Variable in Java

What is a variable in Java?

All computers work with some kind of information. During the execution of a program, all that information is stored in memory and each information gets its address.
The naming of an address is known as a variable.
Later via variables, we retrieve the information we need from memory.

Syntax

type variableName = variableValue;
  • type – a type of information stored in the memory area
  • variableName – variable name
  • variableValue – a value that is stored in memory and that can later be retrieved via the variable name

Once we assign a variable type in Java, we can no longer change it.
If we try to assign a value of the type String to a variable of type int, we will get a compiler error.

Types of Variables in Java

There are three types of variables in Java:

  • instance variable
  • static variable
  • local variable

Instance Variable

Instance variables are declared within a class but outside a constructor or method. They are also called class fields.
Instance variables are used to describe an object.

Example 1:

public class Car {
  
  String brand;
  int maxSpeed;
}

As you can see in example 1, the Car class has two fields, declared brand and maxSpeed ​​that describe the object more closely.

If we create multiple objects of class Car, each object can have its own version of brand and maxSpeed variables.

Example 2:

class Car {
    
  String brand;
  int maxSpeed;
    
  public void printFields() {
    System.out.println("BRAND: " + brand);
    System.out.println("MAX SPEED: " + maxSpeed);
  }
}

public class Test {

  public static void main(String[] args) {
    Car ford = new Car();
    
    ford.brand = "Ford";
    ford.maxSpeed = 320;
    
    Car ferrari = new Car();
    
    ferrari.brand = "Ferrari";
    ferrari.maxSpeed = 370;
    
    System.out.println("FORD: ");
        
    ford.printFields();
        
    System.out.println("FERRARI: ");
        
    ferrari.printFields();
  }
}
Output: FORD: BRAND: Ford MAX SPEED: 320 FERRARI: BRAND: Ferrari MAX SPEED: 370

In example 2, we created two objects of class Car, and each object has its own version of the variables.

Static Variable

static variable is declared with the keyword static, and there will always be only one version of that variable, no matter how many objects are made of that class. Because static variables belong to a class, not an object.

Example 1:

public class Car {
  
  static String brand;
}

The Car class has a declared static variable with the name brand in the above example.

Example 2:

class Car {
    
  static String brand = "Chevrolet";
    
  public void printBrand() {
    System.out.println("BRAND: " + brand);
  }
}

public class Test {

  public static void main(String[] args) {
    Car ford = new Car();
    
    ford.brand = "Ford";
        
    Car ferrari = new Car();
        
    ferrari.brand = "Ferrari";
    
    System.out.println("FORD: ");
    ford.printBrand();
    System.out.println("FERRARI: ");
    ferrari.printBrand();
  }
}

Take a good look at example number 2. What do you think the output will be?

Output:

FORD: 
BRAND: Ferrari
FERRARI: 
BRAND: Ferrari

As we have already said, the static variable is tied to the class itself, so when one object changes its value, it will be changed for all other objects too.

In the above example, we first changed the value of the brand variable to Ford and at that time both objects had the same value. Immediately below we changed the value again, but this time to Ferrari, and thus the value of the brand variable changed for the ford object, too.
And that’s why we got Ferrari twice in the output.

Local Variable in Java

A local variable is a variable that is declared within a method, and it will only be visible in that same method. Also, it only exists in memory during method execution.

public class Car {
    
  int maxSpeed;
   
  public void setMaxSpeed() {
    int localMaxSpeed = 10;
  }
    
  int newMaxSpeed = maxSpeed; // OK
  int getNewMaxSpeed2 = localMaxSpeed; // DOES NOT COMPILE
}

As you can see in the example, we defined one instance variable maxSpeed, and then in the setMaxSpeed ​​method, we created a new, local variable with the name localMaxSpeed.

When we try to assign the value of the local variable to the instance variable, we get a compiler error because localMaxSpeed ​​is not visible outside the method in which we declared it.

That’s it!

 

Leave a Reply

Your email address will not be published.