Home arrow Java arrow Page 6 - Getting Started with Java

Getting Started with Java

This article introduces you to the Java programming language. The first 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).

Author Info:
By: Sams Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 12
May 04, 2006
  1. · Getting Started with Java
  2. · Introduction to Java
  3. · Selecting a Development Tool
  4. · Object-Oriented Programming
  5. · Objects and Classes
  6. · Attributes and Behavior

print this article

Getting Started with Java - Attributes and Behavior
(Page 6 of 6 )

A Java class consists of two distinct types of information: attributes and behavior.

Both of these are present in VolcanoRobot, a project you will implement today as a class. This project, a computer simulation of a volcanic exploration vehicle, is inspired by the Dante II robot used by NASA's Telerobotics Research program to do research inside volcanic craters.

Attributes of a Class of Objects

Attributes are the data that differentiates one object from another. They can be used to determine the appearance, state, and other qualities of objects that belong to that class.

A volcanic exploration vehicle could have the following attributes:

  • Status—exploring, moving, returning home

  • Speed—Measured in miles per hour

  • Temperature—Measured in Fahrenheit degrees

In a class, attributes are defined by variables—places to store information in a computer program. Instance variables are attributes that have values that differ from one object to another.

An instance variable defines an attribute of one particular object. The object's class defines what kind of attribute it is, and each instance stores its own value for that attribute. Instance variables also are called object variables.

Each class attribute has a single corresponding variable; you change that attribute in an object by changing the value of the variable.

For example, the VolcanoRobot class could define a speed instance variable. This must be an instance variable because each robot travels at different speeds depending on the circumstances of the environment. The value of a robot's speed instance variable could be changed to make the robot move more quickly or slowly.

Instance variables can be given a value when an object is created and then stay constant throughout the life of the object. They also can be given different values as the object is used in a running program.

For other variables, it makes more sense to have one value shared by all objects of that class. These attributes are called class variables.

A class variable defines an attribute of an entire class. The variable applies to the class itself and to all its instances, so only one value is stored no matter how many objects of that class have been created.

An example of a class variable for the VolcanoRobot class would be a variable that holds the current time. If an instance variable were created to hold the time, each object could have a different value for this variable, which could cause problems if the robots are supposed to perform tasks in conjunction with each other.

Using a class variable prevents this problem because all objects of that class share the same value automatically. Each VolcanoRobot object would have access to that variable.

Behavior of a Class of Objects

Behavior refers to the things that a class of objects can do to themselves and other objects. Behavior can be used to change the attributes of an object, receive information from other objects, and send messages to other objects asking them to perform tasks.

A volcano robot could have the following behavior:

  • Check current temperature

  • Begin a survey

  • Report its current location

Behavior for a class of objects is implemented using methods.

Methods are groups of related statements in a class of objects that handle a task. They are used to accomplish specific tasks on their own objects and on other objects, and they are used in the way that functions and subroutines are used in other programming languages.

Objects communicate with each other using methods. A class or an object can call methods in another class or object for many reasons, including the following:

  • To report a change to another object

  • To tell the other object to change something about itself

  • To ask another object to do something

For example, two volcano robots could use methods to report their locations to each other and avoid collisions, and one robot could tell another to stop so that it could pass by.

Just as there are instance and class variables, there are also instance and class methods. Instance methods, which are so common they're usually just called methods, are used when you are working with an object of the class. If a method makes a change to an individual object, it must be an instance method. Class methods apply to a class itself.

Be sure to come back next week for the next part of this article.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials