Learn how to store data in objects

The journey from Java wanna-be to Java developer continues

1 2 Page 2
Page 2 of 2
// Bad -- doesn't use encapsulation
public class Person {
  int m_age;
}
public class PersonTest {
  public static void main(String[] args) {
    Person p = new Person();
    p.m_age = -5; // Hey -- how can someone be minus 5 years old?
  }
}
// Better - uses encapsulation
public class Person {
  int m_age;
  public void setAge(int age) {
    // Check to make sure age is greater than 0. I'll talk more about
    // if statements at another time.
    if (age > 0) {
      m_age = age;
    }
  }
}
public class PersonTest {
  public static void main(String[] args) {
    Person p = new Person();
    p.setAge(-5); // Won't have any effect now.
  }
}

Even that simple program shows how you can slip into trouble if you directly access the internal data of classes. The larger and more complex the program, the more important encapsulation becomes. And remember, many programs start out small and then grow to last indefinitely, so design them correctly, right from the beginning. To apply encapsulation to AlarmClock, you can just create methods to manipulate the snooze interval.

Before I proceed, I should discuss methods in more detail. Methods can return values that the caller uses. To return a value, declare a nonvoid return type, and use a return statement. The getSnoozeInterval() method shown in the example below illustrates this.

Write the program

Okay -- you're ready to manipulate the snooze interval. You do this by adding get and set methods for the snooze interval. When you have an instance variable like snoozeInterval, you will regularly call the get and set methods getSnoozeInterval() and setSnoozeInterval().

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);
   }
}
public class AlarmClockTest {
  public static void main(String[] args) {
    // Create two clocks. Each has its own m_snoozeInterval.
    AlarmClock aClock1 = new AlarmClock();
    AlarmClock aClock2 = new AlarmClock();
    // Change aClock2. You use the set method.
    aClock2.setSnoozeInterval(10000);
    aClock1.snooze();    // Snooze with aClock1's interval.
    aClock2.snooze();    // Snooze with aClock2's interval.
  }
}

Defined now are two methods to manipulate the snooze interval. One is used to get the snooze interval, and the other is used to set it. That may seem trivial, but then, AlarmClock is a trivial class. In future columns, the class will grow in functionality and complexity.

Conclusion

You've covered a great deal of new ground. You looked at how to manipulate primitive types like int and double. You examined local variables, method parameters, and variable scope. You learned how to add data to classes using instance variables, and how that data is contained in each instance. Finally, you explored encapsulation and how it leads to better code.

Next time, you'll see some more of the control structures in Java, such as if and while statements, and learn how to enforce encapsulation with Java's access modifiers. You'll also study some of Java's built-in functionality, which can manipulate time, and use it to add more features to the AlarmClock class. To accomplish that, you'll delve more deeply into how one object uses another to complete its work.

double64Double precision floating point (IEEE 754 conforming)
Jacob Weintraub is founder and president of LearningPatterns.com (LPc). Jacob has been working in object technologies since 1989, and teaching Java since 1995. He authored LPc's Java for Programmers, as well as many of its advanced courses, such as those on OOAD and EJB.

Learn more about this topic

Related:
1 2 Page 2
Page 2 of 2