Index
Overview


Before we start playing with objects we need to understand how to use variables.

Let's refresh on the concepts and sytax of variables here....

  • Declare variable data type
  • Declare variable name
1
int
2
width; int height;
Before we use a variable, Java needs to know about the variable (it's data type).

We can use the highlighted width variable only because we have declared it before using it.

Java is a strongly typed language
int width;
int height;

//Use variable "width"
width = 10;
  • Declare variable
  • Use variable

Java executes code sequentially forward or top-down
1
int width; int height;
2
width = 10;
This fails. The variable width is used before it is declared.

  • Use variable (incorrect - used before declared)
  • Declare variable (incorrect - declared after used)
1
width = 10;
2
int width;
First we declare variables.

Then we use the variables.

Java is happy because when we use the vars it already knows about them (via the declarations)
int width;
int height;
int sum;

width = 10;
height = 5;
sum = width + height;