Creating Variables

For the purposes of this hour's examples, you'll be looking at a class of objects called Virus whose sole purpose in life is to reproduce in as many places as possible—much like some of the people I knew in college. A Virus has several different things it needs in order to do its work, and these will be implemented as the behavior of the class. The information that's needed for the methods will be stored as attributes.

Watch Out!

This hour will not teach actual virus writing, though it might provide some insight into how virus programs work as they wreak havoc on the file systems of the computer-loving world. This uploader had scheduled Sams Teach Yourself Virus Programming in a Three-Day Weekend for next spring, but the tutorial has been postponed because the author's hard drive was unexpectedly erased by an email from someone who said "I love you" but had a funny way of showing it.


The attributes of an object represent any variables needed in order for the object to function. These variables could be simple data types such as integers, characters, and floating-point numbers, or they could be arrays or objects of classes such as String or Calendar. An object's variables can be used throughout its program, in any of the methods the object includes. You create variables immediately after the class statement that creates the class and before any methods. One of the things that a Virus object needs is a way to indicate that a file already has been infected. Some computer viruses change the field that stores the time a file was last modified; for example, a virus might move the time from 13:41:20 to 13:41:61. Because no normal file would be saved on the 61st second of a minute, the time is a sign that the file was infected. The Virus object will use 86 as the seconds field of a file's modification time because "86 it" is slang that means to throw something away—exactly the kind of unpleasant antisocial connotation we're going for. The value will be stored in an integer variable called newSeconds. The following statements begin a class called Virus with an attribute called newSeconds and two other attributes:

public class Virus {
 public int newSeconds = 86;
 public String author = "Sam Snett";
 int maxFileSize = 30000;
}


All three variables are attributes for the class: newSeconds, maxFileSize, and author. Putting a statement such as public in a variable declaration is called access control, because it determines how other objects made from other classes can use that variable—or if they can use it at all. The newSeconds variable has a starting value of 86, and the statement that creates it has public as the first part of the statement. Making a variable public makes it possible to modify the variable from another program that is using the Virus object. If the other program attaches special significance to the number 92, for instance, it can change newSeconds to that value. The following statements create a Virus object called influenza and set its newSeconds variable:

Virus influenza = new Virus();
influenza.newSeconds = 92;


In the Virus class, the author variable also is public, so it can be changed freely from other programs. The other variable, maxFileSize, can only be used within the class itself. When you make a variable in a class public, the class loses control over how that variable is used by other programs. In many cases, this might not be a problem. For example, the author variable can be changed to any name or pseudonym that identifies the author of the virus, and the only restriction is aesthetic. The name might eventually be used on court documents if you're prosecuted, so you don't want to pick a dumb one. The State of Ohio v. LoveHandles doesn't have the same ring to it as Ohio v. MafiaBoy. Restricting access to a variable keeps errors from occurring if the variable is set incorrectly by another program. With the Virus class, if newSeconds is set to a value of 60 or less, it won't be reliable as a way to tell that a file is infected. Some files might be saved with that number of seconds regardless of the virus, and they'll look infected to Virus. If the Virus class of objects needs to guard against this problem, you need to do these two things:

  • Switch the variable from public to protected or private, two other statements that provide more restrictive access.
  • Add behavior to change the value of the variable and report the value of the variable to other programs.

A protected variable only can be used in the same class as the variable, any subclasses of that class, or by classes in the same package. A package is a group of related classes that serve a common purpose. An example is the java.util package, which contains classes that offer useful utility functions such as date and time coding and file archiving. When you use the import statement in a Java program with an asterisk, as in import java.util.*, you are making the classes of a package available for use in that program. A private variable is restricted even further than a protected variable—it only can be used in the same class. Unless you know that a variable can be changed to anything without affecting how its class functions, you probably should make the variable private or protected. The following statement makes newSeconds a private variable:

private int newSeconds = 86;


If you want other programs to use the newSeconds variable in some way, you'll have to create behavior that makes it possible. This task will be covered later in the hour. There also is another type of access control: the lack of any public, private, or protected statement when the variable is created. In the programs you have created prior to this hour, you didn't specify any of these statements. When no access control is specified, the variable is available to be used only by any classes in the same package. This is often called default or package access, although there are no statements used to declare it specifically when creating a variable.

      
Comments