Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang Chapter 6 Variable scope and the keyword this.

Similar presentations


Presentation on theme: "Liang Chapter 6 Variable scope and the keyword this."— Presentation transcript:

1 Liang Chapter 6 Variable scope and the keyword this

2 Variable scope A local variable is visible only within the block for which it is declared Inside that block, its name is unique—you may not declare another variable of the same name within that block It's scope is the block within which it is declared

3 Examples of local variable issues for(int i=0; i<5; i++) { x += i; } System.out.println(i);// oops The variable i only exists within the for-loop

4 Examples of local variables A variable declared in a method is local to that method public static String repeatNString(String s, int i){ String result = “”; for (int x = 0; x < I; x++){ result = result + s; } return result; } The result variable can only be used within the repeatNString method!

5 Another example This, however, is OK...... public void goodMethod() { int x; int y; for (int i=0; i<5; i++) { x += i; } for (int i=0; i<5; i++) { y += i; }

6 Global variables An instance variable can be accessed by any method within that class Instance variables are global variables It is possible to declare a local variable with the same name as a global variable If you do....the global variable is hidden! The name will refer to the local variable, and you will probably be confused.......

7 Global variables: example 1 // instance variable declared at the end...... class Circle { double findArea() { return radius*radius*Math.PI; } double radius = 1.0; }

8 Example 2: care required! class Foo { int x = 0; int y = 0; void aMethod() { int x = 1; System.out.println("x is " + x); System.out.println("y is " + y); } This will refer to the x inside the method, not the one outside: it will print out 1, not 0.

9 More name clashes..... If a method-local variable and a global (instance) variable have the same name, only the local variable is visible The instance-level variable is hidden The instance variable can still be accessed using the keyword this

10 Using this public class TestClass{ public int i = 5; void setI(int i){ this.i= i; } … // methods constructors etc. } To refer to the variable belonging to the object from within the object, we can’t use the variable name of the object. Instead, we use the java keyword this. This means “this object”. Suppose we have a class called TestClass, containing an instance variable i. We can refer to this i variable for instances of the class: TestClass testObject = new TestClass(); System.out.println(testObject.i); Suppose the following TestClass definition. Then testObject.i refers to the instance-level variable i in the object testObject. this.i means “the i in this object”: in other words, the instance variable i.

11 Using this in a constructor public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } this.radius is the radius in the object; radius is the variable in the constructor method

12 this has two uses! this.x refers to instance var x of the present object. this() calls the default constructor for an object. This is sometimes handy: it allows complicated constructors to call the default constructor. public class Circle{ private static int NumOfCircles = 0; private double radius; public Circle(){ this.radius = 0.0; NumOfCircles++; } public Circle(double radius){ this(); this.radius = radius; } this() calls the default constructor (which does the NumOfCircles counting). Changes the radius in the created object.


Download ppt "Liang Chapter 6 Variable scope and the keyword this."

Similar presentations


Ads by Google