Presentation is loading. Please wait.

Presentation is loading. Please wait.

Access. Overview Today we will discuss: –How do we access fields and methods? –Why have access restrictions? –What can have access restrictions? –How.

Similar presentations


Presentation on theme: "Access. Overview Today we will discuss: –How do we access fields and methods? –Why have access restrictions? –What can have access restrictions? –How."— Presentation transcript:

1 Access

2 Overview Today we will discuss: –How do we access fields and methods? –Why have access restrictions? –What can have access restrictions? –How do we provide or restrict access?

3 Instance and class variables You can declare variables within a class –These variables are called instance variables, or fields –Every object of that class has its own copy of those fields –The fields describe something about the object You can also declare static variables within a class –There is only one of each static variable –A static variable is also called a class variable –The static variable describes something about the class as a whole

4 Method variables You can declare variables within a method or within a constructor –These are called method variables, not fields –Method variables are basically used for computation –Method variables are strictly temporary, and are used only within that method –When a method returns (completes), all its variables are discarded

5 Example: a “Rabbit” class class Rabbit { static int population; // class variable double hunger; //instance variable double fear; // instance variable double courage = 0.75; // instance variable void eat() { double temp; // method variable temp = courage * hunger; if (temp > fear) { System.out.println(“Eating!”); hunger = hunger - 1; } } }

6 Statements You can declare variables inside a class or inside a method or a constructor You can put statements (executable code) only within methods and constructors, not inside a class Declarations with initializations are still declarations, not statements

7 Statements must be in methods (or in constructors) class Rabbit { double hunger; // OK--declaration double fear = 5.0; // OK--still a declaration hunger = 5.0; // illegal--assignment stmt. Rabbit ( ) { hunger = 5.0; // OK--in a constructor } void eat ( ) { hunger = hunger - 1; // OK--in a method } }

8 Access from inside a class Inside a class, you can access other fields and methods inside the class just by naming them Example: class Person { int age; void birthday( ) { age = age + 1; } void growOlder( ) { birthday( ); } }

9 Accessing from outside a class, 1 Outside a class (from some other class) you access instance variables and methods by –Naming the object you want to talk to –Putting a dot –Naming the variable or method Example: // NOT in class Person, do this: if (john.age < 75) john.birthday();

10 Accessing from outside a class, 2 Outside a class (from some other class) you access class variables and methods by –Naming the class you want to talk to –Putting a dot –Naming the variable or method Examples: Person.population = Person.population + 1; x = Math.abs(y);

11 Responsibility In Java, objects are considered to be active –They have behaviors –They are responsible for their own data –Data (variables) must be kept consistent Example: population should never be negative In order for a class or object to be responsible for its own data, it must keep control of that data

12 Loss of control Suppose a Rabbit object, bugsBunny, has a variable named hunger Inside the class, this method is fine: void eat ( ) { hunger = hunger - 1; } From outside the class, the following is legal: bugsBunny.hunger = bugsBunny.hunger - 1; –But should we be allowed to “reach inside” a rabbit?

13 private variables and methods If you declare a variable or method to be private, that variable or method can only be accessed from within the class –private methods also make sense, e.g. digest() If you declare a variable or method to be public, then any code anywhere can access it Typically, a class or object has both –Methods for use by the rest of the program –Methods and variables that it alone should control

14 Levels of access private -- access only from within the class “package” -- access from within the class, or from any class in the same directory (“folder”) –This is the default; there is no package keyword protected -- access from within the class, or from within any subclass, or from any other class in the same directory public -- access from anywhere at all

15 Levels of access, II To make a variable or method visible –Only within this class: private –From this class and its subclasses: not possible –From this class and its subclasses, and any other class in this directory: “package” (default) –From this subclass and its subclasses, and any other classes in this directory: protected –From anywhere: public

16 Getters and setters One way to control access is via getters and setters: class Rabbit { private double hunger; public getHunger() { return hunger; } public setHunger(double hunger) { this.hunger = hunger; } This seems silly, but it gives great flexibility Probably overkill for small programs

17 Immutable objects Suppose a Planet has a mass, and you want to be able to see its mass but not change it: class Planet { private long mass; // Constructor: Planet(long mass) { this.mass = mass; } //getter long getMass() { return mass; } // Notice there is no setter! }

18 The End


Download ppt "Access. Overview Today we will discuss: –How do we access fields and methods? –Why have access restrictions? –What can have access restrictions? –How."

Similar presentations


Ads by Google