What is a Declaration Statement in Java?

Definition of a Declaration Statement in Java

A declaration statement 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 Declaration Statements

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!", whilst the variable farewellMessage is simply declared as a String.

mla apa chicago
Your Citation
Leahy, Paul. "What is a Declaration Statement in Java?" ThoughtCo, Jan. 10, 2017, thoughtco.com/declaration-statement-2034076. Leahy, Paul. (2017, January 10). What is a Declaration Statement in Java? Retrieved from https://www.thoughtco.com/declaration-statement-2034076 Leahy, Paul. "What is a Declaration Statement in Java?" ThoughtCo. https://www.thoughtco.com/declaration-statement-2034076 (accessed March 20, 2018).