Definition of a Declaration Statement in Java

Java applets

Flickr usuario Silveira Neto

One kind of Java statement is a declaration statement, which is used to declare a variable by specifying its data type and name. Below are some examples of declaration statements.

A variable, in relation to Java programming, is a container that holds values used in a Java program. Instead of defining a value over and over, a variable that has a value attached to it can be defined. Since variables must be given an initial starting value, you can see how that works in the examples on this page.

Examples of Declarations in Java

The following three declaration statements declare int, boolean and String variables:

 int number; 

boolean isFinished;
String welcomeMessage;

In addition to the data type and name, a declaration statement can initialize the variable with a value:

 int number = 10; 

boolean isFinished = false;
String welcomeMessage = "Hello!";

It's also possible to declare more than one variable of the same data type in one declaration statement:

 int number, anotherNumber, yetAnotherNumber; 

boolean isFinished = false, isAlmostFinished = true;
String welcomeMessage = "Hello!", farewellMessage;

The variables number, anotherNumber, and yetAnotherNumber all have int data types. The two boolean variables isFinished and isAlmostFinished are declared with initial values of false and true respectively. Finally, the String variable welcomeMessage is assigned the String value of "Hello!", while the variable farewellMessage is simply declared as a String.

There are also control flow statements in Java as well as expression statements.

mla apa chicago
Your Citation
Leahy, Paul. "Definition of a Declaration Statement in Java." ThoughtCo, Aug. 25, 2020, Leahy, Paul. (2020, August 25). Definition of a Declaration Statement in Java. Retrieved from Leahy, Paul. "Definition of a Declaration Statement in Java." ThoughtCo. (accessed March 21, 2023).