Instance Variables

Instance Variables

They are the local variables, so they only work within the pair of braces that they are declared in including any braces that are inside of that pair of braces where they were declared. This means that if you put it into the body of a method it can only be used in that method and if you declared it inside a for statement or if statement that it can only be used without error within those statements. If when you run a program it says that a variable is out of it’s scope then that means you used the variable in a place outside the braces that it was declared in.

A instance variable can be of any datatype except I assume void since void doesn’t really hold any values. It is also initialized when it is declared meaning that it is assigned a value according to its datatype in its declaration, and this value can be a equation or expression so long as the value/answer of said expression or equation is of the variables datatype . Also when you name a instance variable you have to make sure it isn’t the same name as a field because then it can cause errors or bugs since you won’t be able to use that field inside of that variable scope since to the compiler a local variable takes precedence to a field

A instance variable is declared by:

Datatype variableName = value of datatype;

A instance variable is used in a program simply by using the variableName.

One thought on “Instance Variables

Comments are closed.