Object-oriented language basics, Part 2
Declare and access fields and methods
By Jeff Friesen, JavaWorld.com, 05/04/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Ever hear the expression, "I can't see the forest for the trees?" That expression refers to specific details that cloud your
understanding of the big picture. And what could be cloudier than a detailed examination of fields and methods during an introduction
to Java's classes and objects? That is the reason I chose to minimize fields and methods while maximizing the big picture
-- classes and objects -- in
Part 1 of this object-oriented language series. However, because you eventually have to step back from the forest and focus on the
trees, this month's article examines fields and methods in great detail.
Read the whole series on object-oriented language basics:
Java's variables can be divided into three categories: fields, parameters, and local variables. I'll present fields here and
leave an exploration of parameters and local variables for a later section that discusses methods.
Declaring fields
A field is a variable declared in a class body that holds either part of an object's state (an instance field) or part of a class's
state (a class field). Use the following Java syntax to declare a field in source code:
[ ( 'public' | 'private' | 'protected' ) ]
[ ( 'final' | 'volatile' ) ]
[ 'static' ] [ 'transient' ]
data_type field_name [ '=' expression ] ';'
A field declaration specifies the field's name, data type, optional expression (which initializes the field), access specifiers,
and modifiers. Choose identifiers for the data type and name that are not reserved words. Consider this example:
class Employee
{
String name; // The employee's name.
double salary; // The employee's salary.
int jobID; // The employee's job identification code (e.g., accountant, payroll clerk, manager, and so on).
}
The Employee class declares three fields: name, salary, and jobID. When you create an Employee object, name eventually holds a reference to a String object that contains the employee's name. The salary field will hold the employee's salary, and jobID will hold an integer that identifies the employee's job category.
Access specifiers
You can optionally declare a field with an access specifier keyword: public, private, or protected. The access specifier determines how accessible the field is to code in other classes. Access ranges from totally accessible
to totally inaccessible.
If you do not declare a field with an access specifier keyword, Java assigns the field a default access level, which makes
the field accessible within its class and to all classes within the same package. (Think of packages as libraries of classes
-- a concept I'll explore in a future article.) Any class not declared in the same package as the class that contains the
field cannot access the field. Take a look at the following example:
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Download this article's source code and resource files
http://www.javaworld.com/javaworld/jw-05-2001/java101/jw-0504-java101.zip
- For a glossary specific to this article, homework, and more, see the Java 101 study guide that accompanies this article
http://www.javaworld.com/javaworld/jw-05-2001/jw-0504-java101guide.html
- The Java Language Specification, Second Edition, James Gosling, Bill Joy, Guy Steele, Gilad Bracha (Sun Microsystems, 2000)
http://www.javasoft.com/docs/books/jls/second_edition/html/j.title.doc.html
- The Java Tutorial
http://www.javasoft.com/tutorial/
- Sun's official Java language definition
http://java.sun.com/docs/overviews/java/java-overview-1.html
- Learn more about singletons in Joshua Fox's article "When is a Singleton not a Singleton," (JavaWorld, January 12, 2001)
http://www.javaworld.com/jw-01-2001/jw-0112-singleton.html
- Find out how the compiler handles constructors in "Constructor Help," Tony Sintes (JavaWorld, November 22, 2000)
http://www.javaworld.com/javaqa/2000-11/04-qa-1122-constructor.html
- Read "Understanding Constructors," Robert Nielsen (JavaWorld, October 13, 2000) to learn more about constructors
http://www.javaworld.com/jw-10-2000/jw-1013-constructors.html
- Need more help with Java? Please visit the Java Beginner discussion
http://www.itworld.com/jump/jw-0504-java101/forums.itworld.com/webx?230@@.ee6b804
- Sign up for the JavaWorld This Week free weekly email newsletter and keep up with what's new at JavaWorld
http://www.idg.net/jw-subscribe
- Check out past Java 101 articles
http://www.javaworld.com/javaworld/topicalindex/jw-ti-java101.html
- For more articles geared toward the Java beginner, browse the Intro Level section of JavaWorld's Topical Index
http://www.javaworld.com/javaworld/topicalindex/jw-ti-introlevel.html
- Java experts answer your toughest Java questions in JavaWorld's Java Q&A column
http://www.javaworld.com/javaworld/javaqa/javaqa-index.html
- For Tips 'N' Tricks see
http://www.javaworld.com/javaworld/javatips/jw-javatips.index.html