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

Learn Java from the ground up

An introduction to Java that will help Java newbies to become Java developers

  • Print
  • Feedback

Page 5 of 6


new AlarmClock();


This creates an alarm clock off the heap. There's still a problem here, however. Unlike cookies we can just pick up and put on a cookie sheet (and then pick up and eat), objects float around in a computer's memory, and we need to have a way to get to them. Java uses special variables called object references to refer to objects. You can initialize a reference to refer to a given object, and then use the reference to interact with that object. Here's what this looks like:


AlarmClock aClock = new AlarmClock();


The first AlarmClock declares the type of the variable; aClock is the name of the variable; and new AlarmClock() creates a new alarm clock that is then used to initialize aClock.

Just for fun, let's add a little behavior to our alarm clock. You add behavior in methods. We'll add the snooze method, which will just print a message to the console as our first program did. Methods go inside classes, and here's what one looks like:


public void snooze() { System.out.println("ZZZZZ"); }


When we put it in our AlarmClock class, we get the following:


public class AlarmClock { public void snooze() { System.out.println("ZZZZZ"); } }


Does AlarmClock.java contain a program? No, it doesn't, because there is no main method in class AlarmClock. Remember that a Java program is just a class with a main method in it.

Let's create another program called AlarmClockTest. This just means we'll create a class AlarmClockTest with a main method in it. We'll create an instance of AlarmClock in this program.

public class AlarmClockTest {
    public static void main(String[] args) {
      AlarmClock aClock = new AlarmClock();
    }
}



Last, but not least, we want to do work with this alarm clock. The way you tell an object to do something is by calling, or, invoking, one of its methods. You generally do this through the variable that refers to it, and you use the dot operator for this. All we can do is snooze with this clock, but hey, that's everybody's favorite action with an alarm clock anyway. Here's what the code looks like:

public class AlarmClockTest {
     public static void main(String[] args) {
         AlarmClock aClock = new AlarmClock();
         aClock.snooze();
     }
}


Ready? Create AlarmClockTest.java, and type in the code above. Compile it (javac AlarmClockTest.java) as well as AlarmClock.java. You're ready to run your new program. The main method is in AlarmClockTest class, so this is how you run the program:


java AlarmClockTest


You should see ZZZZZ printed out on the command line. Congratulations, you've now completed your second Java program!

Review

We covered quite a bit here. We looked at the roots of Java, and some of its underlying architecture. We've examined the basics of object-oriented programming and how you use OOP in Java. We also created a Java class, and we've written a couple of short Java programs.

We skipped over a lot of detail and moved very quickly so we could get to some of the more interesting aspects of Java programming, like creating classes and calling methods, in this first column. I'll be going into much more detail on all of this in the future.

  • Print
  • Feedback

Resources