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

Page 2 of 7

class MyClass
{
   int fred;
}


Only code contained in MyClass and other classes declared in the same package as MyClass can access fred.

If you declare a field private, only code contained in its class can access the field. That field becomes inaccessible to every other class in every other package. Examine the code below:

class Employee
{
   private double salary;
}


Only code contained in Employee can access salary.

If you declare a field public, code contained in its class and all other packages' classes can access the field. Refer to the code below:

public class Employee
{
   public String name;
}


Code contained in Employee and all other packages' classes can access name. (Employee must also be declared public before code in other packages can access name.)

Declaring every field in a given class public defeats the concept of information hiding. Suppose you create a Body class to model the human body and Eye, Heart, and Lung classes to model an eye, the heart, and a lung. The Body class declares Eye, Heart, and Lung reference fields, as demonstrated by the following example:

public class Body
{
   public Eye leftEye, rightEye;
   private Heart heart;
   private Lung leftLung, rightLung;
}


The leftEye and rightEye field declarations are public because a body's eyes are visible to an observer. However, the heart, leftLung, and rightLung declarations are private because the organs they represent are hidden inside the body. Suppose heart, leftLung, and rightLung were declared public. Wouldn't that be the equivalent to having a body with its heart and lungs exposed?

Finally, a field declared protected resembles a field with the default access level. The only difference between the two access specifiers is that subclasses in any package can access the protected field. The following example demonstrates that:

public class Employee
{
   protected String name;
}


Only code contained in Employee, other classes declared in the same package as Employee, and all Employee's subclasses (declared in any package) can access name.

Modifiers

You can optionally declare a field with a modifier keyword: final or volatile and/or static and/or transient.

If you declare a field final, the compiler ensures that the field is initialized and subsequently treats the field as a constant -- a read-only variable. The compiler can now perform internal optimizations on a program's byte codes because it knows the constant will not change. Consider the example below:

class Employee
{
   final int ACCOUNTANT = 1;
   final int PAYROLL_CLERK = 2;
   final int MANAGER = 3;
   int jobID = ACCOUNTANT;
}


The example above declares three final int fields: ACCOUNTANT, PAYROLL_CLERK, and MANAGER.

Note: It is customary to use all capital letters and separate multiple words with underscores when declaring constants. That helps distinguish constants from read/write variables when analyzing source code.

If you declare a field volatile, multiple threads can access the field, and certain compiler optimizations are prevented so that the field is accessed appropriately. (You'll learn about volatile fields when I discuss threads in a future article.)

  • Print
  • Feedback

Resources