Home News DOC Class DL Others Cooker
Documents  >  Tutorial  >  Declaration
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 of the declaration statement is:


Class-name Variable-name ;


It is a statement ending with a semicolon after specifying the class name and variable name.

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

You can access 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

It 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.

Next
Previous
Copyright © Rice All rights reserved.