How to use Java variables

Java variables are containers that can store different Java data types. They include local variables, instance variables and static variables. While the size of Java variables cannot be changed after the fact, you can change their content after creating them.

What are Java variables?

Every programming language has variables that are used to work with the code in a program. Variables are containers that store data of a specific data type (and only data of that type). In Java, variables can contain Java’s primitive data types like whole numbers, floating point numbers, truth values and individual digits. They can also store complex data types like Java strings. Variables in Java have a specific, clearly defined size that can’t be changed after the fact. However, the contents of a variable can be changed later. In this tutorial, we introduce the different variable types and show how to create variables for different data types.

Web Hosting
Secure, reliable hosting for your website
  • 99.9% uptime and super-fast loading
  • Advanced security features
  • Domain and email included

How to declare and initialise var in Java

The first thing you’ll need to know is how to declare and initialise Java variables. This process is the same for all types of variables. To declare a variable you’ll need two parameters. The first is the data type that you want to store in the variable. The second is the name of the variable.

When you then initialise the variable, there are 3 ingredients. You’ll not only need the data type and name of the variable as above, but also a value for it. Initialising a variable amounts to assigning a value to a previously blank variable.

Here’s what the syntax looks like:

Datatype Name = Value;
java

What are the three variable types in Java?

There are three types of variables in Java: local variables, instance variables and static variables.

Local variables

Local variables in Java are declared in the body of a method, constructor or block. The variable can then only be used within that method. Here’s how that looks in practice:

public class Main {
	public static void main(String[] args) {
		int var = 5;
		System.out.println("The local variable is: " + var);
	}
}
java

The output will look as follows:

The local variable is: 5
java

Instance variables

Instance variables are created inside a class but outside a method, constructor, or block. They come about when you create an object with the keyword ‘new’. Unlike local variables, instance variables have standard values. For numbers, the standard value is 0 or 0.0. For Booleans, it is false. For object references, it is null.

In the following example, you can see how instance variables are used in Java. In the code we’ll create a group of people that want to contribute to a birthday gift and list what each person has contributed.

public class Gift  {
	public String name;
	private double contribution;
	public Gift (String person) {
		name = person;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getContribution() {
		return contribution;
	}
	public void setContribution(double participantContribution) {	
		contribution = participantContribution;
	}
	public static void main(String[] args) {
		Gift participant = new Gift ("Peter");
		participant.setContribution(10);
		System.out.println("Name: " + participant.getName());
		System.out.println("Contribution = " + participant.getContribution());
		Gift participant2 = new Gift ("Maria");
		participant2.setContribution(15);
		System.out.println("Name: " + participant2.getName());
		System.out.println("Contribution  = " + participant2.getContribution());
	}
}
java

Here’s the output for this code:

Name: Peter
Contribution = 10.0
Name: Maria
Contribution = 15.0
java

Static variables

While the other two Java variable types cannot be declared statically, static variables can. They are declared outside of a method, constructor or block and thus belong to the class and not to an instance. They are used by all the instances in a class. Static variables are allocated memory when the class is loaded in the memory. Here’s an example in code:

public class Gift {
	public static String participant = "Peter";
	public static void main(String[] args) {
		System.out.println("Participant: " + Gift.participant);
	}
}
java

Here’s the output:

Participant: Peter
java

How to create variables with different data types

The process of creating Java variables is pretty similar across data types. We’ll show you some examples for the most common data types and explain their differences.

boolean

A Boolean can only contain the truth values true or false. It’s declared as follows:

public class Main {
	public static void main(String[] args) {
		boolean pizzaTastesGood = true;
		System.out.println(pizzaTastesGood);
	}
}
java

Here’s the output:

true
java

int

int is the most commonly used data type for whole numbers. You can declare a Java var for an int like this:

public class Main {
	public static void main(String[] args) {
		int x = 10;
		System.out.println(x);
	}
}
java

Here’s the output:

10
java

float

float is used for decimal numbers. Here’s how you can declare a float var in Java:

public class Main {
	public static void main(String[] args) {
		float x = -17.03f;
		System.out.println(x);
	}
}
java

Here’s the output:

-17.03
java

char

char contains a single character that’s notated in single quotation marks. Here’s how that looks in code:

public class Main {
	public static void main(String[] args) {
		char x = 'S';
		System.out.println(x);
	}
}
java

Here’s the output:

S
java

String

In addition to the primitive data types we looked at above, Java variables can also contain complete strings. Strings should be entered in double quotation marks:

public class Main {
	public static void main(String[] args) {
		String example = "This is a string in Java.";
		System.out.println(example);
	}
}
java

Here’s the output:

This is a string in Java.
java
Was this article helpful?
Page top