Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topics to cover Instance variables vs. local variables.

Similar presentations


Presentation on theme: "Topics to cover Instance variables vs. local variables."— Presentation transcript:

1 Topics to cover Instance variables vs. local variables

2 A new method in the Person class
public String predictFuture () { int magicNumber = (int) (Math.random() * 4 + 1); String prediction = "Yours is the life of a student"; switch(magicNumber) { case 1: prediction = "You will do well in life"; break; case 2: prediction = "You will be rich and famous"; break; case 3: prediction = "Happiness will be yours"; break; case 4: prediction = "You will be a programmer"; break; case 5: prediction = "Life is not kind"; break; } return prediction;

3 Local Variables Declared within the body of a method
Have “local scope” Can only be used in the method in which they are declared Local variables can be declared in any block of code

4 Instance Variables Are declared at the start of the class definition outside of any interior block of code Are used to store the “state” of an object Can be used by any method in the class Have unique values for each object


Download ppt "Topics to cover Instance variables vs. local variables."

Similar presentations


Ads by Google