go to previous page   go to home page   go to next page hear noise

Answer:

Every application so far in these notes starts with

public static void main ( String[] args ) 

This identifies the main method in the class.


Class

When a Java application is run, objects are created and their methods are invoked (are run). To create an object, you need a description of it.

A class is a description of a kind of object. A programmer may define a class using Java, or may use predefined classes that come in class libraries.

A class is merely a plan for a possible object. It does not by itself create any objects. When a programmer wants to create an object the new operator is used with the name of the class. Creating an object is called instantiation.

Here is a tiny application that instantiates an object by following the plan in the class String:


class StringTester
{

  public static void main ( String[] args )
  {
    String str1;   // str1 is a variable that may refer to an object. 
                   // The object does not exist unit the "new" is executed.
    int    len;    // len is a primitive variable of type int

    str1 = new String("Random Jottings");  // create an object of type String
                                                           
    len  = str1.length();  // invoke the object's method length()

    System.out.println("The string is " + len + " characters long");
  }
}

When it is executed, the line

str1 = new String("Random Jottings");

creates an object of the class String. The class String is defined in the class library java.lang that comes with the Java system. The computer system finds a chunk of memory for the object, lays out this memory according to the plan (the definition of String), and puts data and methods into it.

The data in this String object are the characters "Random Jottings" and the methods are the methods that all String objects have. One of these methods is length().

The variable str1 is used to refer to this object. In other words, str1 gives the object a name.


QUESTION 7:

If a program has a name for an object, does that mean that an object really exists?