Tutorials About
Java Language

1 Java Tutorial
2 What is Java?
3 Java Core Concepts
4 Java Syntax
5 Java Variables
6 Java Data Types
7 Java Arrays
8 Java Strings
9 Java Operations
10 Java if statements
11 Java Switch Statements
12 Java for Loops
13 Java while Loops
14 Java Classes
15 Java Fields
16 Java Methods
17 Java Constructors
18 Java Packages
19 Java Access Modifiers
20 Java Inheritance
21 Java Nested Classes
22 Java Abstract Classes
23 Java Interfaces
24 Java Interfaces vs. Abstract Classes
25 Java Enums
26 Java Annotations
27 Java Main Program
28 Java Lambda Expressions
29 Java Exercises

Java Variables

A Java variable is a piece of memory that can contain a data value. A variable thus has a data type. Data types are covered in more detail in the text on Java data types.

Java Variable Types

In Java there are four types of variables:

  • Non-static fields
  • Static fields
  • Local variables
  • Parameters

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.

A static field is a variable that belongs to a class. A static field has the same value for all objects that access it. Static fields are also called class variables. Static fields are also covered in more detail in the text on Java fields.

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 are also only accessible inside the method that declares them, although a value is assigned to them when the method is called. Parameters are also covered in more detail in the text on Java methods.

Java Variable Declaration

Exactly how a variable is declared depends on what type of variable it is (non-static, static, local, parameter). However, there are certain similarities that

In Java you declare a variable like this:

type name ;

Instead of the word type, you write the data type of the variable. Similarly, instead of the word name you write the name you want the variable to have.

Here is an example declaring a variable named myVariable of type int.

int myVariable;

Here are examples of how to declare variables of all the primitive data types in Java:

byte    myByte;
short   myShort;
char    myChar;
int     myInt;
long    myLong;
float   myFloat;
double  myDouble;

Here are examples of how to declare variables of the object types in Java:

Byte       myByte;
Short      myShort;
Character  myChar;
Integer    myInt;
Long       myLong;
Float      myFloat;
Double     myDouble;
String     myString;

Notice the uppercase first letter of the object types.

When a variable points to an object the variable is called a "reference" to an object. I will get back to the difference between primitive variable values and object references in a later text.

Java Variable Assignment

In Java you assign a value to a variable like this:

myByte   = 127;

myFloat  = 199.99;

myString = "string value";

You can also assign a value to a variable already when it is declared. Here is how that is done:

byte   myByte   = 127;

float  myFloat  = 199.99;

String myString = "string value";

Java Variable Reading

You can read the value of a Java variable by writing its name where a variable or constant variable can be used in the code. For instance, as the right side of a variable assignment, as parameter to a method call, or inside a arithmetic expression. For instance:

float myFloat1 = 199.99;

float myFloat2 = myFloat1;           // right hand side value in assignment

float myFloat3 = myFloat2 + 123.45;  // as part of arithmetic expression

System.out.println(myFloat3);        // as parameter in method call.

Java Variable Naming Conventions

There are a few rules and conventions related to the naming of variables.

The rules are:

  1. Variable names are case sensitive. The name money is not the same as Money or MONEY.
  2. Variable names must start with a letter, or the $ or _ character.
  3. After the first character in a variable name, the name can also contain numbers (in addition to letters, the $, and the _ character).
  4. Variable names cannot be equal to reserved key words in Java. For instance, the words int or for are reserved words in Java. Therefore you cannot name your variables int or for.

There are also a few variable naming conventions. Conventions are not necessary to follow. The compiler to not enforce them. But it may still be a good idea to follow them. The conventions are:

  • Variable names are written in lowercase. For instance, variable or money.
  • If variable names consist of multiple words, each word after the first word has its first letter written in uppercase. For instance, variableName or pocketMoney.
  • Even though it is allowed, you do not normally start a variable name with $ or _ .
  • Static final fields (constants) are named in all uppercase, typically using an _ to separate the words in the name. For instance EXCHANGE_RATE or COEFFICIENT.

Connect with me: Newsletter - Get all my free tips!

This website uses cookies to improve the user experience and gather statistics. Our advertisers use cookies too (3rd party cookies), to provide more relevant ads. Continued use of this website implies that you accept the use of cookies on this website. We do not share our cookies with our advertisers, and our advertisers do not share cookies with us.