Newsletter sign-up
View all newsletters

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

Class action

Discover more about creating Java classes

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
In the last Java 101 column, I presented the different primitive types that Java supports. I also discussed variables, their scope and characteristics, and the various places where developers can implement them. In addition, I introduced encapsulation and the idea of using accessor methods to manipulate a class's instance variables instead of accessing them directly from outside the class.

By the end of the article, we had developed a class that looked like this:

public class AlarmClock { 
  long m_snoozeInterval = 5000; // Snooze time in millisecond 
  // Set method for m_snoozeInterval. 
  public void setSnoozeInterval(long snoozeInterval) { 
    m_snoozeInterval = snoozeInterval; 
  } 
  // Get method for m_snoozeInterval. 
  // Note that you are returning a value of type long here. 
  public long getSnoozeInterval() { 
    // Here's the line that returns the value. 
    return m_snoozeInterval; 
  } 
  public void snooze() { 
    // You can still get to m_snoozeInterval in an AlarmClock method 
    // because you are within the scope of the class. 
    System.out.println("ZZZZZ for: " + m_snoozeInterval); 
   } 
} 


Access modifiers and enforcing encapsulation

A small problem presents itself in our current AlarmClock class. Though we decided to use encapsulation and we created accessor methods for our snooze interval, we haven't yet included a way to ensure that programmers don't directly access m_snoozeInterval. Java provides an easy approach to completing this important requirement: the use of access modifiers.

You include access modifiers in the declaration of a field (a member variable or method) to control access to it. We will cover the public and private modifiers now, and other classifications in a later article.

  • The private access modifier: You can only access a private field from within the class that defines it. Only the methods of the class itself, or other parts of the class, can access the field.
  • The public access modifier: You can access a public field in the same places where you access the class that defines it. When you access the class, you can reach any public members within it.


You should not allow public access to variables, as it exposes your implementation to the world and nullifies the purpose of encapsulation.

Here's how we would restrict access to m_snoozeInterval and allow access to getSnoozeInterval():

public class AlarmClock { 
  // Restrict access to m_snoozeInterval via private
  private long m_snoozeInterval = 5000;
  // Allow access to getSnoozeInterval via public
  public long getSnoozeInterval() { 
    return m_snoozeInterval; 
  } 
  // Remainder of class as before
        
}


The Java compiler will now flag as an error any attempt to access m_snoozeInterval from code that falls outside the class. Because of the private modifier, the following code is erroneous:

public class AlarmClockTest {
  public static void main(String[] args) {
    AlarmClock ac = new AlarmClock();
        
    long snoozeInterval = ac.m_snoozeInterval;  // ERROR - Won't compile
  }
}


On the other hand, the code below is correct. We can still call getSnoozeInterval() from outside the class, which then returns the snooze interval to us.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources
  • Previous Java 101 columns