different types of variables in Java

Java Data Types (Primitive and Non primitive / Reference), Java Variables (Local Variables, Instance Variables and Static or Class Variables), Explicit Declaration of Data Types & Variables, Purpose of Variables, Variable naming restrictions and Data Conversion.
Post Reply
Posts: 87
Joined: Fri Nov 23, 2018 5:29 am

different types of variables in Java

Post by manoj » Tue Dec 04, 2018 10:16 am

What are different types of variables in Java?

Posts: 134
Joined: Fri Nov 23, 2018 2:52 am

Re: different types of variables in Java

Post by swathi » Tue Dec 04, 2018 10:18 am

There are different types of variables in Java. They are as follows:

1. Instance Variables (Non-Static Fields)
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. For example, the currentSpeed of one bicycle is independent from the currentSpeed of another.

2. 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.

3. Local Variables
A method stores 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—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.

4. Parameters
They are the variables that are passed to the methods of a class.

Variable Declaration
Identifiers are the names of variables. They must be composed of only letters, numbers, the underscore, and the dollar sign ($). They cannot contain white spaces. Identifiers may only begin with a letter, the underscore, or the dollar sign. A variable cannot begin with a number. All variable names are case sensitive.

Syntax for variable declaration
datatype1 variable1, datatype2 variable2, … datatypen variablen;

For example:

int a, char ch;
Variables can be assigned values in the following way: Variablename = value;

For example;


Posts: 55
Joined: Thu Jan 03, 2019 3:57 am

Re: different types of variables in Java

Post by kishore » Sat Jan 05, 2019 5:36 am

In Java there are four types of variables:
Non-static fields:
A non-static field is a variable that belongs to an object.
Objects keep their internal state in non-static fields.
Non-static fields are also called instance variables, because they belong to instances (objects) of a class.
Non-static fields are covered in more detail in the text on Java fields.

Static fields:
Static variables are also known as class variable because they are associated with the class and common for all the instances of class.
For example, If I create three objects of a class and access this static variable, it would be common for all, the changes made to the variable using one of the object would reflect when you access it through other objects.

Local variables:
A local variable is a variable declared inside a method.
A local variable is only accessible inside the method that declared it.
Local variables are covered in more detail in the text on Java methods.

A parameter is a variable that is passed to a method when the method is called Parameters.
They are also only accessible inside the method that declares them, although a value is assigned to them when the method is called Parameters.

Post Reply

Return to “Java Data Types & Variables”