Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Class action

Discover more about creating Java classes

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 5

public class AlarmClockTest {
  public static void main(String[] args) {
    AlarmClock ac = new AlarmClock();
        
    long snoozeInterval = ac.getSnoozeInterval();  // OK
  }
}


Access modifiers are a simple but powerful concept. By ensuring that other programmers follow the rules of encapsulation, it helps you write stronger code. In addition, as part of the Java security mechanisms, it aids in ensuring that code won't access sensitive parts of the Java system.

Now you understand what the public modifier before all our methods signifies. But how do we access fields that have neither a public nor a private declaration? Good question! We'll cover that in a future column when we talk more about packages.

Set the alarm clock

Our alarm clock is turning into a pretty nifty device, yet it still lacks some key functions. For example, how do we set the alarm? Let's add that functionality now.

We'll set the alarm with methods that allow us to designate the hour and minute that we want the alarm to go off. We'll assume that users will tell time with a 24-hour time system, in which time proceeds from 00:00 (0 hours and 0 minutes) to 23:59 (23 hours and 59 minutes). The hour from midnight to 1 a.m. is hour 0, and the hour from noon to 1 p.m. is hour 12, which means 23:59 is 11:59 p.m. in 12-hour clock time.

We will add methods with which you can set the hour and minute separately. For convenience, we'll also include a method to set them both at once. In addition, we will add instance variables to hold the alarm time. Our AlarmClock class will now look like this:

public class AlarmClock { 
  private long m_snoozeInterval = 5000; // Snooze time in millisecond 
  // Instance variables for alarm setting 
  private int  m_alarmHour = 0;
  private int  m_alarmMinute = 0;
  // Set method for m_snoozeInterval. 
  public void setSnoozeInterval(long snoozeInterval) { 
    m_snoozeInterval = snoozeInterval; 
  } 
  // Get method for m_snoozeInterval. 
  public long getSnoozeInterval() { 
    return m_snoozeInterval; 
  } 
  // Getter and setter methods for alarm settings
  public void setAlarmHour(int hour) {
    m_alarmHour = hour;
  }
  public int getAlarmHour() {
    return m_alarmHour;
  }
  public void setAlarmMinute(int minute) {
    m_alarmMinute = minute;
  }
  public int getAlarmMinute() {
    return m_alarmMinute;
  }
  // Set alarm hour and minute together
  // Note that we call our other setter methods to do this
  // We'll discuss why later.
  public void setAlarmTime(int hour, int minute) {
    setAlarmHour(hour);
    setAlarmMinute(minute);
  }
  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); 
   } 
} 


Though our alarm clock features rather elementary functions, it continues to grow in complexity. Type it out yourself and give it a whirl.

public class AlarmClockTest {
  public static void main(String[] args) {
    AlarmClock ac = new AlarmClock();
    // It's an early day tomorrow - let's set the alarm.
    ac.setAlarmHour(6);
    ac.setAlarmMinute(15);
    System.out.println("Alarm setting is " + ac.getAlarmHour() + ":" +
                        ac.getAlarmMinute());
  }
}


Initialization and constructors

It is extremely important that developers learn how to initialize Java objects. You initialize objects through constructors, which are special methods called when an object is created. Every class in Java can have constructors.

  • 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