Presentation is loading. Please wait.

Presentation is loading. Please wait.

{ int fun = 99; } Scope refers to whether the variable is seen within a certain context Any variable defined inside of curly brackets, only exists within.

Similar presentations


Presentation on theme: "{ int fun = 99; } Scope refers to whether the variable is seen within a certain context Any variable defined inside of curly brackets, only exists within."— Presentation transcript:

1

2 { int fun = 99; } Scope refers to whether the variable is seen within a certain context Any variable defined inside of curly brackets, only exists within those braces. That variable has a scope limited to those brackets.

3 for (int i=0; i

4 When you need many methods to have access to the same variable, you should make that variable an instance variable. The scope of an instance variable is the entire class where that variable is defined.

5 public class InstanceVars { private int one = 8, two = 3; //instance variables one, two, total private int total = 0; //exist throughout the class public void add() { total = one + two; } public void print() { System.out.println(total); } public static void main(String args[]) { InstanceVars test = new InstanceVars(); test.add(); test.print(); } OUTPUT 11

6 int num; int num = 99; num = 56; definition and assignment assignment only definition only Defining and declaring mean the same thing

7 When you need only one method to have access to a variable, you should make that variable a local variable. The scope of a local variable is limited to the method where it is defined.

8 public class LocalVars { private int fun; //instance variable public void change() { int fun = 99; //defining/assigning a value to local variable } public void print() { System.out.println(fun); } public static void main(String args[]) { LocalVars test = new LocalVars(); test.change(); test.print(); } OUTPUT 0

9 public class LocalVars { private String phrase = “Instance variable”; //instance variable public void change() { String phrase = “Local variable”; //local variable } public void print() { System.out.println(phrase); } public static void main(String args[]) { LocalVars test = new LocalVars(); test.change(); test.print(); } OUTPUT Instance variable

10 public class LocalVars { private int fun; //instance variable public void change() { fun = 99; //assigning a value to instance variable } public void print() { System.out.println(fun); } public static void main(String args[]) { LocalVars test = new LocalVars(); test.change(); test.print(); } OUTPUT 99

11 When you need to access an instance variable that has the same name as a local variable in a method, you can prefix the instance variable with this. For example, if you want to access an instance variable named age, you can use this.age

12 public class ThisVar { private int fun; //instance variable public void change() { int fun = 99; //assigning a value to local variable this.fun = fun - 20; //assigning a value to instance variable } public void print() { System.out.println(fun); } public static void main(String args[]) { LocalVars test = new LocalVars(); test.change(); test.print(); } OUTPUT 79

13 class Rectangle { int length, breadth; public Rectangle (int length,int breadth) { this.length=length; this.breadth=breadth; } int calculate() { return(length*breadth); } public static void main(String[] args) { Rectangle rectangle=new Rectangle(5,6); int area = rectangle.calculate(); System.out.println("The area is " + area); } } OUTPUT The area is 30 Typical usage of this

14


Download ppt "{ int fun = 99; } Scope refers to whether the variable is seen within a certain context Any variable defined inside of curly brackets, only exists within."

Similar presentations


Ads by Google