Presentation is loading. Please wait.

Presentation is loading. Please wait.

 The position of a method in a program is not critical.  Why?

Similar presentations


Presentation on theme: " The position of a method in a program is not critical.  Why?"— Presentation transcript:

1  The position of a method in a program is not critical.  Why?

2  Variables can exist for: ◦ The lifetime of the program ◦ The lifetime of a method  This is known as the ‘scope’ of a variable.

3  Variable can be used: ◦ Throughout the entire program ◦ In only one method  These types of variable are (respectively): ◦ Global (instance) ◦ Local

4  A variables scope is determined by where in the program it is declared

5  Variables declared inside the body of a method are usable only in that method.  The variables, known as local variables, will disappear when the method is finished.

6  Variables declared outside any method but inside the body of a class are usable by every method.  These variables, known as global variables, will disappear only when the object is destroyed  In object orientation global variables are usually referred to as instance variables.

7  Once arguments are passed to a method, they are local to the method (local variables), once the method has completed its operation, these local variables are removed from memory.  Instance variable are different to local variables as they are declared outside of methods and are thus available to all methods in the class - instance variables are not removed from memory until the object itself is.  Scope is the part of a program in which a variable exists and can be used. If you try to use a variable outside of its scope, an error will occur

8 public class scopeClass { int globalVar;//instance variable with global scope public static void main(String args[]) { scopeClass scopeClassObject = new scopeClass(); } public scopeClass() { int localVar;//local scope localVar = 1; globalVar = 4; System.out.println(globalVar+":"+localVar); compute(); System.out.println(globalVar+":"+localVar); } public void compute() { this.globalVar = globalVar + 1; }

9  Variables names should always be unique, however if variables can be distinguished in a way other than their names the compiler will allow two variables with the same name.  Two variables with different scope can have the same name.  Note: although permitted doing this is not recommended as it often leads to confusion for readers of a program.

10  Two forms of visibility in an object ◦ Internal – what can be seen by the methods inside the object: Local & Global  Controlled by placing variables in relevant positions ◦ External – what can be seen by other objects:Public and Private  Controlled using the visibility modifiers public & private

11  We have seen the idea of public/ private, we now look at it in more detail

12  Public is the equivalent of making something publicly available, e.g. publishing it on a web site or in a newspaper.

13  Private methods and variables can only be accessed by methods within the object.

14  Person object:  Public attributes – things people can get at without your permission ◦ Phone number ◦ Car registration ◦ Name  Private attributes – things no-one can get without your express permission ◦ ATM password

15  Note that there is a distinct difference between the scope of a variable and its modifier.  Local variables do not need modifiers as they can never be seen outside the object since they are contained within a method. The method that contains the local variables will need a modifier.  Global variables do need modifiers as they are outside all the methods and therefore are part of the object thus needing an indication as to whether they are public or private.

16  One of the benefits of classes is that you can protect member variables and methods from access by other objects (variables in particular)  It is good programming practice to make instance variables private and to access/modify the values of these using public getter and setter methods  Private: A private member is accessible only to its own class and nowhere else. Using the private modifier is the main way in which an object encapsulates itself.  Useful when:  other classes have no reason to use a variable/method  an inappropriate value could cause major problems


Download ppt " The position of a method in a program is not critical.  Why?"

Similar presentations


Ads by Google