|
|
The only part of your code in an application that is required to be static is the method main, as this is invoked from the start. The method main is defined in the main class of your project. So far in this book, all of the examples have simply consisted of one solitary main class defining static members. For those of you who are not used to object-oriented programming, this may seem quite normal. Just start in main, declare global methods and variables in the main file, and then go from there, but this is merely escaping the point of OOP in general—using objects.
So let's say we want to make a class to represent a creature, like a human or alien or some other intelligent life form ("intelligent" used loosely). We could make an Alien class containing a string value for greeting us in its native language. The following class is an example of how we would implement this class, as we have been doing so far in this book.
public class Alien { public static void main(String args[]) { greeting = "Dak-Dak-DaDakDakDak"; System.out.println("Alien says: "+greeting); } static String greeting; }
Just enter this basic code for now into a source file Alien.java, and we will adapt it accordingly throughout this section.
The class Alien contains the String member greeting, which is static. The problem is that our Alien class basically represents one alien because greeting is static. We can still create objects of the Alien class at the moment, however, similar to when we created objects of the String class in the previous chapter. For example, we could add the following code somewhere in main.
Alien martian = new Alien();
This code declares a reference to an Alien object called martian and then assigns it to reference a newly created Alien object. The constructor invoked is a default constructor for the object, as the object does not define one of its own (this was discussed in detail in the constructor section at the beginning of Chapter 2).
However, we want to make many instances of the Alien class, each having its own copy of the variable greeting. So we need to make the variable greeting non-static. This simply entails removing the keyword static from the declaration of greeting. We can also create our own constructor for the Alien class that takes a String argument value to assign to greeting.
So in the example Alien.java, we can adapt the Alien class so that it is suitable for creating individual Alien objects, as follows.
public class Alien { // constructor public Alien(String greeting) { this.greeting = greeting; } public static void main(String args[]) { // create two new aliens Alien martian = new Alien("Dak-Dak-DaDakDakDak"); System.out.println("First alien says: " + martian.greeting); Alien plutonian = new Alien("Hi, erm... I'm from Pluto"); System.out.println("Second alien says: " + plutonian.greeting); } String greeting; }
When you run this code, you should get output similar to the following screen shot.
Figure 4-1:
Here we create two new Alien objects from the entry point of the program in the method main. You should treat main as almost a separate method from the main class; it may seem strange that main is inside the main class and not separate, but in Java all code must be contained within the code block of a class or interface, even main (we shall see about interfaces toward the end of this chapter). The code for this example is quite straightforward apart from one new keyword, this, which we have used in the constructor.
|
|