您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)


Reflect Index

Reflect Page # 2

Discussion Topic 1 — Access methods

It is normal for a Java class to restrict access to its class and instance variables. For example, if class X wants to get the value of a variable in class Y, it would normally call a method (an accessor method) in class Y to do this.

Although it is possible to read an instance variable in a different object directly, like this:

int minutes = calendar.minutes;

We would normally use an accessor method, like this:

int minutes = calendar.get (Calendar.MINUTE);

Working this way leads to a slight increase in the complexity of every class, as accessor methods need to be provided to control access to a number of variables.

So why is this way of working so widespread? What advantages does it have?

Contribution for Discussion Topic 1 ?Access methods

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg