Java
  Home arrow Java arrow 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 
Sun Developer Network 
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


    (Page 1 of 7 )

    This article introduces you to the Java programming language. The second of two parts, it 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).

    Creating a Class

    To see classes, objects, attributes, and behavior in action, you develop a VolcanoRobot class, create objects from that class, and work with them in a running program.


    Note - The main purpose of this project is to explore object-oriented programming. You'll learn more about Java programming syntax during Day 2, "The ABCs of Programming."


    To begin creating a class, open the text editor you're using to create Java programs and create a new file. Enter the text of Listing 1.1, and save the file as VolcanoRobot.java in a folder you are using to work on programs from this book.

    Listing 1.1 The Full Text of VolcanoRobot.java

     1: class VolcanoRobot {
    2:   String status;
    3:   int speed;
    4:   float temperature;
    5: 
    6:   void checkTemperature() {
    7:     if (temperature > 660) {
    8:       status = "returning home";
    9:       speed = 5;
    10:     }
    11:   }
    12: 
    13:   void showAttributes() {
    14:     System.out.println("Status: " + status);
    15:     System.out.println("Speed: " + speed);
    16:     System.out.println("Temperature: " +
    temperature); 17: } 18: }

    The class statement in line 1 of Listing 1.1 defines and names the VolcanoRobot class. Everything contained between the opening brace ("{") on line 1 and the closing brace ("}") on line 18 is part of this class.

    The VolcanoRobot class contains three instance variables and two instance methods.

    The instance variables are defined in lines 2–4:

    String status;
    int speed;
    float temperature;

    The variables are named status, speed, and temperature. Each is used to store a different type of information:

    • status holds a String object, a group of letters, numbers, punctuation, and other characters.

    • speed holds an int, an integer value.

    • temperature holds a float, a floating-point number.

    String objects are created from the String class, which is part of the Java class library and can be used in any Java program.


    Tip - As you might have noticed from the use of String in this program, a class can use objects as instance variables.


    The first instance method in the VolcanoRobot class is defined in lines 6–11:

    void checkTemperature() {
    if (temperature > 660) {
    status = "returning home";
    speed = 5;
    }
    }

    Methods are defined in a manner similar to a class. They begin with a statement that names the method, the kind of information the method produces, and other things.

    The checkTemperature() method is contained within the brackets on lines 6 and 11 of Listing 1.1. This method can be called on a VolcanoRobot object to make sure the robot hasn't become overheated.

    This method checks to see whether the object's temperature instance variable has a value greater than 660. If it does, two other instance variables are changed:

    • The status is changed to the text "returning home", indicating that the temperature is too hot and the robot is heading back to its base.

    • The speed is changed to 5. (Presumably, this is as fast as the robot can travel.)

    The second instance method, showAttributes(), is defined in lines 13–17:

    void showAttributes() {
    System.out.println("Status: " + status);
    System.out.println("Speed: " + speed);
    System.out.println("Temperature: " + temperature);
    }

    This method uses System.out.println() to display the values of three instance variables along with some text explaining what each value represents.

    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-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT