Connect with us

Programming Languages

What is a variable in JAVA?

A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type.

funkyprogrammer_99902

Published

on

Variable

Variable is name of reserved area allocated in memory. In other words, it is a name of memory location. It is a combination of “vary + able” that means its value can be changed.

Variable in Java is a data container that stores the data values during Java program execution. Every variable is assigned data type which designates the type and quantity of value it can hold. Variable is a memory location name of the data. The Java variables have mainly three types : Local, Instance and Static.

Variable Declaration:

To declare a variable, you must specify the data type & give the variable a unique name.

Examples of other Valid Declarations are

int a,b,c;

float pi;

double d;

char a;

Variable Initialization:

To initialize a variable, you must assign it a valid value.

Example of other Valid Initializations are

pi =3.14f;

do =20.22d;

a=’v’;

The Java programming language defines the following kinds of variables:

  • Instance Variables (Non-Static Fields) Technically speaking, objects store their individual states in “non-static fields”, that is, fields declared without the static keyword. Non-static fields are also known as instance variables because their values are unique to each instance of a class (to each object, in other words); the currentSpeed of one bicycle is independent from the currentSpeed of another.
  • Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances. The code static int numGears = 6; would create such a static field. Additionally, the keyword final could be added to indicate that the number of gears will never change.
  • Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.
  • Parameters You’ve already seen examples of parameters, both in the Bicycle class and in the main method of the “Hello World!” application. Recall that the signature for the main method is public static void main(String[] args). Here, the args variable is the parameter to this method. The important thing to remember is that parameters are always classified as “variables” not “fields”. This applies to other parameter-accepting constructs as well (such as constructors and exception handlers) that you’ll learn about later in the tutorial.

If you want to learn our JAVA, PHP , PYTHON course click on button given bellow :

Continue Reading

Copyright © 2021 By Funky Programmer.