Sobes.tech
Junior — Middle

Can you list different types of variables used in Java?

sobes.tech AI

Answer from AI

In Java language, the following main variable types exist:

  • Local variables — declared inside methods, accessible only within the method, have no default value and must be initialized before use.
  • Instance variables (class fields) — declared inside a class but outside methods, belong to a specific object, have default values (e.g., 0 for numbers, null for objects).
  • Static variables (static fields) — belong to the class itself, not to objects, shared among all instances of the class.
  • Method parameters — variables passed to a method during invocation.

Example:

public class Example {
    static int staticVar = 10; // static variable
    int instanceVar;           // instance variable

    public void method(int param) { // method parameter
        int localVar = 5;           // local variable
        System.out.println(localVar + param + instanceVar + staticVar);
    }
}
Can you list different types of variables used in… - sobes.tech