Declaration statement

Declaration statement is a statement to introduce variables into a program.


Table of contents:


Format

Class name

Variable name

Semicolon

Example

Format

The format is:


Class-name Variable-name ;


This is a statement ends with a semicolon after specifying the class name and variable name.

This statement introduces a variable into a particular scope and binds the specified class to the variable.

You can access to the variable by name within the declared scope.


Variables can be declared any number in any location. However, it is not possible to declare a variable with the same name in the same scope.

Variables with the same name can be declared if scope is different. In this case, the variable declared at the inside scope hides the outside variable with same name.

Class name

It represents a class that is bound to a variable.

Variable name

This is an identifier used to access a variable.

Semicolon

The end of a declaration statement is represented by a semicolon.

Example

1:

class example

2:

int x;

3:

int var1;

4:

open method void someMethod()

5:

int x; // ♦1

6:

int var2;

7:

if(true)

8:

int x; // ♦2

9:

int var3;

10:

endif

11:

endmethod

12:

ec

All are declarations for variables of the int class. These variables can only handle instances of the int class.

♦1 and ♦2 declare variables with the same name in different scope. Each hides the outer variable with the same name.

Copyright © Rice All rights reserved.