1. Technology

Declaration Statement



A declaration statement is used to declare a variable by specifying its data type and name. For example, 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. And finally, the String variable welcomeMessage is assigned the String value of "Hello!", whilst the variable farewellMessage is simply declared as a String.


# A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

  1. About.com
  2. Technology
  3. Java
  4. Java Glossary
  5. D
  6. What Is a Declaration Statement in Java?

©2014 About.com. All rights reserved.