Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Object-oriented language basics, Part 2

Declare and access fields and methods

  • Print
  • Feedback
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:

  • Print
  • Feedback

Resources