In Java, a declaration statement is a type of statement that declares a new variable, method, or class.
Here are some examples of declaration statements in Java:
// Declare a new integer variable int x; // Declare a new string variable and assign it an initial value String s = "Hello World"; // Declare a new method public void myMethod(int x, String s) { // method body } // Declare a new class public class MyClass { // class body }
In these examples, the first statement declares a new integer variable x
, the second statement declares a new string variable s
and assigns it an initial value, the third statement declares a new method myMethod
with two parameters, and the fourth statement declares a new class MyClass
.
Declaration statements are used to create new variables, methods, and classes that can be used in the code. They are an important part of the Java language and are used to define the structure and behavior of a Java program.