{ int fun = 99; } scope refers to whether the variable is seen within a certain context any variable...

14

Upload: millicent-strickland

Post on 16-Dec-2015

225 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: { 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
Page 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

{ 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.

Page 3: { 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

for (int i=0; i<list.size(); i++){ System.out.println(list.get(i));}

for (int i=0; i<5; i++){ System.out.println(i*10);}

System.out.println("i="+i);

This applies to for loops. Variable i has a scope limited to the for loop in which it is declared.

Compilation error. i is not in scope here.

Variable i is in scope within the for loop’sbrackets

Variable i is in scope within the for loop’sbrackets

Page 4: { 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

When you need many methodsto have access to the samevariable, you should make that variable an instance variable.

The scope of an instance variableis the entire class where that variable is defined.

Page 5: { 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

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

Page 6: { 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

int num;

int num = 99;

num = 56;

definitionand assignment

assignment only

definition only

Defining and declaring mean the same thing

Page 7: { 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

When you need only one methodto have access to a variable, you should make that variablea local variable.

The scope of a local variable islimited to the method where itis defined.

Page 8: { 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

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

Page 9: { 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

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(); }}

OUTPUTInstance variable

Page 10: { 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

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

Page 11: { 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

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

Page 12: { 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

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

Page 13: { 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

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

Page 14: { 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