Java
  Home arrow Java arrow Page 2 - Introducing Classes and More in Java
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Introducing Classes and More in Java
By: Sams Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 2
    2006-05-11

    Table of Contents:
  • Introducing Classes and More in Java
  • Running the Program
  • Organizing Classes and Class Behavior
  • Creating a Class Hierarchy
  • Inheritance in Action
  • Summary
  • Questions and Answers

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introducing Classes and More in Java - Running the Program


    (Page 2 of 7 )

    If you have compiled the VolcanoRobot class, you can't actually use it to simulate the actions of these exploratory robots. The class you have created defines what a VolcanoRobot object would be like if it were used in a program, but it doesn't use one of these objects yet.

    There are two ways to put this VolcanoRobot class to use:

    • Create a separate Java program that uses this class.

    • Add a special class method called main() to the VolcanoRobot class so that it can be run as an application, and then use VolcanoRobot objects in that method.

    The first option is chosen for this exercise. Listing 1.2 contains the source code for VolcanoApplication, a Java class that creates a VolcanoRobot object and uses its instance variables and methods.

    Listing 1.2 The Full Text of VolcanoApplication.java

     1: class VolcanoApplication {
    2:   public static void main(String[] arguments) {
    3:     VolcanoRobot dante = new VolcanoRobot();
    4:     dante.status = "exploring";
    5:     dante.speed = 2;
    6:     dante.temperature = 510;
    7: 
    8:     dante.showAttributes();
    9:     System.out.println("Increasing speed to
    3."); 10: dante.speed = 3; 11: dante.showAttributes(); 12: System.out.println("Changing temperature to
    670."); 13: dante.temperature = 670; 14: dante.showAttributes(); 15: System.out.println("Checking the
    temperature."); 16: dante.checkTemperature(); 17: dante.showAttributes(); 18: } 19: }

    Save the file as VolcanoApplication.java and compile the program.

    If you are using the Software Development Kit, you can do the following to compile the program: Go to a command line or open a command-line window, open the folder where VolcanoApplication.java was saved, and then compile the program by typing the following command at the command line:

    javac VolcanoApplication.java

    The Java compiler also compiles the VolcanoRobot.java file if necessary because the VolcanoRobot class class is being used in this application.


    Tip - If you encounter problems compiling or running any program in this book with SDK 1.5, you can find a copy of the source file and other related files on the book's official Web site at http://www.java21days.com.


    After you have compiled the application, run the program.

    Using the SDK, you can run the VolcanoApplication program by opening the folder containing the VolcanoRobot.class and VolcanoApplication.class files at a command line and using this command:

    java VolcanoApplication

    When you run the VolcanoApplication class, the output should be the following:

    Status: exploring
    Speed: 2
    Temperature: 510.0
    Increasing speed to 3.
    Status: exploring
    Speed: 3
    Temperature: 510.0
    Changing temperature to 670.
    Status: exploring
    Speed: 3
    Temperature: 670.0
    Checking the temperature.
    Status: returning home
    Speed: 5
    Temperature: 670.0

    Using Listing 1.2 as a guide, the following things take place in the main() class method:

    • Line 2—The main() method is created and named. All main() methods take this format, and you'll learn more about them during Day 5, "Creating Classes and Methods." For now, the most important thing to note is the static keyword. This indicates that the method is a class method.

    • Line 3—A new VolcanoRobot object is created using that class as a template. The object is given the name dante.

    • Lines 4–6—Three instance variables of the dante object are given values: status is set to the text "exploring", speed is set to 2, and temperature is set to 510.

    • Line 8—On this line and several that follow, the showAttributes() method of the dante object is called. This method displays the current values of the instance variables status, speed, and temperature.

    • Line 9—On this line and others that follow, a System.out.println() statement is used to display the text within the parentheses.

    • Line 10—The speed instance variable is set to the value 3.

    • Line 13—The temperature instance variable is set to the value 670.

    • Line 16—The checkTemperature() method of the dante object is called. This method checks to see whether the temperature instance variable is greater than 660. If it is, status and speed are assigned new values.

    More Java Articles
    More By Sams Publishing


       · This article is an excerpt from the book "Sams Teach Yourself Java 2 in 21 Days, 4th...
     

    Buy this book now. This article is excerpted from chapter one of Sams Teach Yourself Java 2 in 21 Days, 4th Edition, written by Rogers Cadenhead and Laura Lemay (Sams; ISBN: 0672326280). Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek