Anda di halaman 1dari 24

Objects

and Classes
Members, variables, and parameters About reference types

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Announcements
A new format for this Thursdays quiz
Open book, take-home quiz Due Friday in lab NO collaboraPon allowed.

Mid-Semester exam next week


Closed book, but you can bring 1 hand-wriRen A4 sheet of notes, which you will hand-in with the exam

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

MEMBERS, VARIABLES & PARAMETERS


G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011 3

Referring to the members of an object


There is a dierence between how we refer to the members of an object from within and outside the instance methods of a class. Outside instance methods of the class (e.g. in another class), we use the syntax:
<object>.member e.g. student1.name()! Where <object> is an instance of the class

Inside an instance method of the class, we just use the member directly e.g. name(), knowing that it refers to the current object
G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011 4

Example
Class deniPon:
public class Student {! private String name;! ! public void setName(String n){! !name = n;! }! ! public String getName() {! !return name;! }! ! public void sayHello() {! !System.out.println(Hello, I am + getName());! }! }!

NoPce that inside the instance methods of the class, we refer to member variables and methods directly
G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011 5

Example

Test program (with main method):

public class Test {! ! Class deniPon: public static void main(String[] args){! !Student stud = new Student();! public class Student {! ! private String name;! !stud.setName(Ama);! ! !! System.out.print(Students name is );! public void setName(String n){ !System.out.println(stud.getName());! !name = n;! ! }! !stud.sayHello();! ! public String getName() {! }! }! !return name;! }! ! public void sayHello() {! !System.out.println(Hello, I am + getName());! }! }!

NoPce that inside the instance methods of the class, we refer to member variables and methods directly

NoPce that outside the class, we instanPate an object of the class and use a dot . to refer to its members
6

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Referring to the members of an object, conPnued


We said that inside an instance method of the class, we just use the member directly e.g. name(), knowing that it refers to the current object AlternaPvely, from inside an instance method of the class we can refer to member of the class by using the keyword this e.g: this.name()!

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Example: the keyword this!


Class deniPon:
public class Student {! private String name;! ! public void setName(String n){! !this.name = n;! }! ! public String getName() {! !return this.name;! }! ! public void sayHello() {! !System.out.println(Hello, I am + this.getName());! }! }!

OpPonally, inside the instance methods of the class, we refer to member variables using the keyword this!
G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011 8

Instance Variables versus Local Variables


An instance variable is a variable that is a member of an object
It will exist as long as the object exists We say the variables scope is inside the class

A local variable is a variable that is declared inside a method of the object


It exists only while the method is being called We say the variables scope is inside the method

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Example: Instance vs. Local Variables


public class Student {! private String name;! name is an instance/member variable.! ! public void setName(String n){! !this.name = n;! }! ! public void readName(){! !Scanner in = new Scanner(System.in);! !String tempName = in.next();! !name = tempName;! tempName is a local variable. It exists }! only in this method and will no longer ! exist ager this method is done! public String getName() {! !return this.name;! To keep the value of the name read in }! for future use, we store it in the member ! variable.! public void sayHello() {! !System.out.println(Hello, I am + this.getName());! }! }!
G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011 10

Method parameters
Note that the input parameters for methods are similar to local variables in that their scope is within the method for which they are dened
public class Student {! private String name;! ! public void setName(String n){! !this.name = n;! }! ! public String getName() {! !return this.name;! }! }!

n is an input parameter for the method setName. Its scope is within setName and nothing more.!

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

11

More about Variable Scope


The scope of a variable is the segment of code within which it is valid As a rule of thumb, the scope of a variable is between the code block within which it is declared
A block of code is demarcated by opening or closing braces, a class, a method, or a loop construct

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

12

What is the Scope of the Following Variables?


age n text year

public class Person {! private int age;! ! Person(){! !age = 0;! }! ! public void grow(){! !age++;! }! ! public void growNYears(int n) {! String text = I am + age + yrs old.;! !System.out.println(text);! ! !for (int year=1; year <= n; year++){! ! !grow();! ! !text = In year + year + I am + age;! ! !System.out.println(text);! !}! !text = Done growning.;! !System.out.println(text);! }! }!
13

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

What is the Scope of the Following Variables?


age: Inside the Person class n: Inside the growNYears() method text: Inside the growNYears() method year: Inside the for- loop

public class Person {! private int age;! ! Person(){! !age = 0;! }! ! public void grow(){! !age++;! }! ! public void growNYears(int n) {! String text = I am + age + yrs old.;! !System.out.println(text);! ! !for (int year=1; year <= n; year++){! ! !grow();! ! !text = In year + year + I am + age;! ! !System.out.println(text);! !}! !text = Done growning.;! !System.out.println(text);! }! }!
14

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Eect of variables with the same name


Note that you cannot have variables of the same name inside the same scope However, be careful of having variables of the same name is a more restricted scope the variable in the more restricted scope will hide the variable in the less restricted scope

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

15

Variable Hiding Example


Warning!!! The local variable age is hiding the member variable age. The eect is that ager this method is done, the member variable age will sPll have its old value, not the new value that was read in.

public class Person {! private int age;! ! Person(){! !age = 0;! }! ! public void readAge(){! !Scanner in = new Scanner(System.in);! !int age = in.nextInt();! }! ! public void grow(){! !age++;! }! ! public int getAge(){! !return age;! }! }!

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

16

Variable Hiding Example - Fixed


But sPll, try not to hide variables! Warning!!! The local variable age is hiding the member variable age. However, we can sPll refer to the member variable by using the this keyword. In this statement, the right- hand-side refers to the local variable age and the leg-hand- side refers to the member variable age.

public class Person {! private int age;! ! Person(){! !age = 0;! }! ! public void readAge(){! !Scanner in = new Scanner(System.in);! !int age = in.nextInt();! !this.age = age;! }! ! public void grow(){! !age++;! }! ! public int getAge(){! !return age;! }! }!

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

17

Another Example of Variable Hiding and Using the this Keyword


public class Student {! private String name;! ! public void setName(String name){! !this.name = name;! }! ! public void readName(){! !Scanner in = new Scanner(System.in);! !String name = in.next();! !this.name = name;! }! ! public String getName() {! !return name;! }! }!

Warning!!! The parameter name is hiding the member variable name. Here, the right-hand-side refers to the parameter name and the leg-hand-side refers to the member variable name. Warning!!! Local variable name is hiding member variable name. Here, the right-hand-side refers to the local variable name and the leg-hand-side refers to the member variable name.
18

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Variable Hiding Try not to do it!


Try not to give your parameters and local variables the same names as your member variables it is hard to keep track and make sure youre doing the right thing Note, however, that you can have local variables with the same name in dierent methods this is not variable hiding because each variable has its own scope

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

19

Example of Variables with the Same Name, In Dierent Scopes


public class Student {! private String name;! ! public void setName(String newName){! !name = myName;! }! ! public void readName(){! Note that each of the variables/ !Scanner in = new Scanner(System.in);! parameters called newName has !String newName = in.next();! !name = newName ;! its own scope they do not }! interfere with each other. ! public String appendName(String suffix) {! !String newName = name + suffix;! !name = newName! }! }!

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

20

ABOUT REFERENCE TYPES

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

21

Reference Types
Remember when you use the keyword new to create an object, Java actually returns a reference to the object You can think of a reference as a memory address Thus, this is a fundamental dierence between a variable of a primiPve type and a variable of a class type
A variable of a primiPve type stores a value A variable of a class type stores a reference

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

22

Values vs. References: Example


Suppose we have a Person class with the member variables age and height (both integers) Consider the following snippet of code: int x;!
Person person;!

We know that Java allocates memory for the two variables and iniPalizes them to their default values
Suppose the variables are allocated start at memory addresses 1024 and 1028 respecPvely
Memory address 1024 Memory address 1028

0 null

x person Note: null is a special value meaning empty reference


23

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Values vs. References: Example


Now, suppose we assign to our two variables x = 13;! person = new Person();! Remember that Java stores values for primiPve types
So, the value 13 will be put in the memory locaPon for x
Memory address 1024

13

However, Java stores references for class types.


Java creates a new Person() object, for example at locaPon 2048 in memory Java stores the reference to the Person object in the variable person
Memory address 1028

2048

person

Memory address 2048

age height
24

G. Ayorkor Korsah, Ashesi University College, Programming II - First Semester 2011

Anda mungkin juga menyukai