Core Java Concepts and Syntax

Types of Variables in Java

Types of Variables in Java

Introduction

The definition of a variable is to set aside memory for the values to be stored. Depending on the kind of variable, they can either be initialized at the time of definition or afterward. In Java, there are primarily three types of variables: static variables, instance variables, and local variables. In JAVA, there are 3 different kinds of variables:

Variable Naming Conventions

There are laws and standards specific to each programming language regarding the kinds of names you are permitted to use, and Java is no exception. You may sum up the guidelines and practices for calling your variables as follows:

  • Case matters when naming variables. Any valid identifier, including an unlimited string of Unicode characters and digits starting with a letter, a dollar sign "$," or the underscore character "_," may be used as the name of a variable.
  • However, it is customary to always start your variables with a letter rather than "$" or " ." Additionally, it is customary to never use the dollar sign symbol. The dollar symbol may occasionally appear in automatically produced names, but it should never appear in variable names.
  • Similar rules apply to the underscore character; although it is theoretically OK to start the name of your variable with "_," doing so is not recommended. There can be no white space.
  • Letters, numbers, dollar signs, and underscore characters are all acceptable as additional characters. This rule is also subject to conventions and common sense. Instead of adopting cryptic acronyms, use whole words when naming your variables.
  • Your code will be simpler to read and comprehend if you do this. In many circumstances, it will also enable your code to function as its own documentation; for instance, fields called cadence, speed, and gear are considerably more understandable than their shorter counterparts, s, c, and g.
  • The name you select must not be a keyword or a restricted word, either.

Java Variable Types

1. Local variables

  • They may also be referred to as stack variables. Since they are stored in stack memory, initializing the local variable is necessary.
  • Otherwise, the compiler will issue a run-time error.
  • These can be specified in a block, a function, or a method. When a method ends, the scope or lifetime of the local variable is destroyed.

2. Instance variables

  • Member variables and fields are other names for instance variables.
  • These are connected to the production of objects. Instance variables are also created as the object is formed.
  • These are heap-based. If you don't explicitly set the initial value for an instance variable, it will implicitly receive the default value at runtime.

3. Class Variables OR Static Variables

  • When a class is loaded into the JVM, they are loaded and initialized.
  • The class variable only exists once. They are heap memory creatures.
  • These variables have an implicit default value assigned to them if they are not initialized.

Examples Of Variables

The software presented here explains the three types of variables that are given. JDK (java development kit) and an editor are required to review the output from a java application once it has been written ( such as NetBeans IDE, Eclipse IDE or VSCode/Notepad++). The Eclipse editor is utilized in this idea to verify the output for various types of variables.

The program file will be immediately saved in TypeOfVariable.java when you open the Eclipse editor, start a new project, and name the class TypeOfVariable. This occurs because of how Java programming is done. The name of the class with the main function declared and the.java extension must be saved in the file name.

Code Example demonstrating the behavior of the 3 variable types

public class TypeOfVariable{
public static int staticvariable;
int instancevariable;
public void printValue(){
int localvariable;
System.out.println("the value of staticvariable \t"+staticvariable);
System.out.println("the value of instancevariable\t"+instancevariable);
System.out.println("the value of localvariable \t"+localvariable);
}
public static void main(String args[]){
TypeOfVariable object=new TypeOfVariable();
object.printValue();
}
}

write your code here: Coding Playground

Three variables are defined as a static, instance, and local variables, respectively, in this program. They are called “staticvariable”, “instancevariable”, and “localvariable”.

Please Note: A compile-time error will occur if you execute this program without initializing the local variable. This is straightforward because if the local variable is not initialized, an error message such as "The variable may not have been initialized" will appear.

Correct Program Code With Initialization of Local Variable

In the same program, merely assign “localvariable” its initial value. The significance of this change will automatically be reflected in the changed output. Considering that only local variables need to be initialized with a value. Class/Static and instance variables automatically give these particular variables the null /0/ false value.

public class TypeOfVariable{
public static int staticvariable;
int instancevariable;
public void printValue(){
int localvariable = 10;
System.out.println("the value of staticvariable \t"+staticvariable);
System.out.println("the value of instancevariable\t"+instancevariable);
System.out.println("the value of localvariable \t"+localvariable);
}
public static void main(String args[]){
TypeOfVariable object=new TypeOfVariable();
object.printValue();
}
}

Our Output for above program is given here:
the value of staticvariable     0
the value of instancevariable    0
the value of localvariable     10

write your code here: Coding Playground