Skip to main content

If you don't have an IBM ID and password, register here.

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

Effective field visibility in Java programs

Identifying, implementing, and accessing fields

Scott W. Ambler, Practice Leader, Agile Development, Rational Methods Group, IBM, Software Group
Scott W. Ambler is President of Ronin International, a consulting firm specializing in object-oriented software process mentoring, architectural modeling, and Enterprise JavaBeans (EJB) development. He has authored or co-authored several books about object-oriented development, including the recently released The Object Primer 2nd Edition, which covers, in detail, the subjects summarized in this article. He can be reached at scott.ambler@ronin-intl.com and at his Web site at www.ambysoft.com.

Summary:  Fields, also known as attributes or member attributes, are the data aspects of objects. A field's visibility defines the level of access to it by Java objects. This week's discussion, modified from Chapters 7 and 8 of The Object Primer 2nd Edition, focuses on the types of field visibility, how to implement fields, and how to access them.

Date:  01 Sep 2000
Level:  Introductory

Comments:  

Table 1 describes the three types of field visibility, indicating the Unified Modeling Language (UML) symbols, the Java keywords, a description of what the visibility means, and my suggested proper usage for that visibility. As you can see, the rules and notations for field visibility are consistent with those for member function visibility, as discussed last week.

Table 1. Visibility of fields in Java programs

VisibilityUML SymbolJava keywordDescriptionSuggested usage
Public+publicA public field can be accessed by any other method in any other object or class.Don't make fields public.
Protected#protectedA protected field can be accessed by any method in the class in which it is declared or by any method defined in subclasses of that class. Don't make fields protected.
Private-privateA private field can only be accessed by methods in the class in which it is declared, but not in the subclasses.All fields should be private and accessed by getter and setter methods (accessors).

In my experience, all fields should be declared private for purposes of information hiding and encapsulation. When fields are declared protected, there is the possibility that methods in subclasses will directly access them, effectively increasing the coupling within a class hierarchy. This increased coupling makes your classes more difficult to maintain and enhance; therefore, it should be avoided.

To show you how to implement fields with different visibilities, I present the declarations for the fields of the Address class in Figure 1. It is possible to declare fields that are public or protected, as you can see in the source code example, but that is rarely a good idea.


Figure 1. Declaring the fields of the Address class

/**
* The city street of the address
*
* @example 1701 Enterprise Way
*/
private String street;

/**
* The name of the city
*
* @example Metropolis
*/
protected String city;

/**
* The state/province that the address is in
*/
public State state;

/**
* The ZIP code that the address is in
*
* @example 90210
*/
private String zipCode;

Fields should never be accessed directly; instead, accessors should be used. Accessor methods come in two flavors: setters and getters (sometimes referred to as mutators and accessors, respectively). A setter modifies the value of a field, whereas a getter obtains its value. The only time that I would consider going against this rule is when the accessor methods are clearly impacting the performance of my application and I have no other alternative for improving the performance to an acceptable level -- even then I would attempt to minimize the number of fields that I access directly. By the way, I've been working in Java technology since the autumn of 1995 and have yet to break this rule. At the same time, I've lost track of the number of developers I've met who complain about accessor method performance.

An essential concept that Java programmers must learn is that the only accessor methods that are allowed to work directly with a field are the accessors themselves. Yes, it is possible to directly access a private field within the methods of the class in which the field is defined, but you don't want to do so because you would increase the coupling within your class. In fact, the use of accessors is a practice that is enforced in Enterprise JavaBean (EJB) 2.0 environments for all persistent fields. The effective use of accessor methods will be covered in future tips.


Resources

About the author

Scott W. Ambler is President of Ronin International, a consulting firm specializing in object-oriented software process mentoring, architectural modeling, and Enterprise JavaBeans (EJB) development. He has authored or co-authored several books about object-oriented development, including the recently released The Object Primer 2nd Edition, which covers, in detail, the subjects summarized in this article. He can be reached at scott.ambler@ronin-intl.com and at his Web site at www.ambysoft.com.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in

If you don't have an IBM ID and password, register here.


Forgot your IBM ID?


Forgot your password?
Change your password


By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)


By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=SOA and web services
ArticleID=10467
ArticleTitle=Effective field visibility in Java programs
publish-date=09012000
author1-email=scott_ambler@ca.ibm.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).