CSE 115 Tutorial 3 Variables

Designer: Yingrui Liu

  Declaration
Home
Declaration
Assignment
Scope & lifetime
Relationship & Methods

Generally, a declaration of a variable consists of a type and an identifier. The simplest form is,

type identifier;

type: type of the thing we want to store, usually the type is a class name
identifier: name that programmer choose to give to the variable. Though there is no rules for what kind of name we should give to the variable, we usually choose a name that describe the function of the variable so that it could be easy to understand and make changes afterwards for us.

When we declared a variable, the computer allocate a part of the memory to install the variable.

Declaring local variable

Example:
         Elephant elephant;
type: Elephant    
Notice: In the program given at home page, we imported the class so that we don't need to mention it in declaring. But the standard way to write the type is lab4lib.Elephant."lab4lib" is the class of "Elephant." It is the same when declaring other kind of variables.
identifier: elephant
Naming Covention: Begin with a lower case letter, and the first letter of each subsequent word in method name is capitalized.

Declaring instance variable

Example:
       private WindowWithButtons _windowWithButtons;
access-cotrol-modifier: private
A little different is that the declaration include an "access-control-modifier" which is usually "private."In this way the access to this variable is restricted to the class in which it is defined. type: WindowWithButton
identifier: _windowWithButton
Naming Convention: follow the same general naming conventionw as names of local variables, with the exception that their first character is an underscore character '_'.         

Declaring parameter

Example:
     public CreateElephantListener(WindowWithButtons windowWithButtons)
The content within the "( )" is the declaration of parameter.
type: WindowWithButtons
identifier: windowWithButtons


back to top