{ int fun = 99; } scope refers to whether the variable is seen within a certain context any variable...
TRANSCRIPT
{ 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.
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
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.
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
int num;
int num = 99;
num = 56;
definitionand assignment
assignment only
definition only
Defining and declaring mean the same thing
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.
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
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
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
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
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
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