Let's Draw! (More on using new)

The best way to begin looking at the details of new is with a code example, so let's make a Point. We will also draw our code so we can see what is happening to the Point object.

Code Example: Creating a Point Object

    Point p1;
    
    p1 = new Point();
    System.out.println("The Point p1 is: " + p1);

    p1.setLocation(10, 200);
    System.out.println("The Point p1 is now: " + p1);

Let's draw the objects for this code (this is also called hand simulation). The idea is that you manually go through each line of code and draw a picture of all the objects based on executing the code, one line at a time.

  1. The first bolded line is a declaration. It declares an object called p1 of class Point. Remember that objects are references, and since this object does not have memory yet, it refers to nothing. In Java, a reference with no memory is set to the value null. This is how we would draw the object at this point in the code:

    Since the object has no memory, it also has no value - you cannot manipulate it until it does. If you try, the Java compiler will give you an error message stating that the variable may not have been initialized and your new .class file will not be created.

  2. Next, we use new to allocate memory for the object. The assignment statement stores the reference to this new memory in the object p1. After the keyword new is a call to a constructor. A constructor is a method with the same name as the class, in this case Point. A constructor's job is to initialize the object. So what value do you think p1 is set to?

    We would update out picture to look like this:

    p1 is initialized to a default value of 0 for the x coordinate and 0 for the y coordinate. Let's check it by printing out p1. The output would be this:

    The Point p1 is: java.awt.Point[x=0,y=0]

  3. Next, we give the point a new value by calling the setLocation() method. Notice we are using dot-notation. The basic syntax for dot-notation is

    object.method(args)

    Since the method call is the entire statement, we end the statement with a semicolon. In this call, the object is p1, the method is setLocation, and the two arguments represent the new x and y coordinates for the point. We would update our picture to look like this:

    Let's print p1 again and check its value after the call to setLocation(). The output would be this:

    The Point p1 is now: java.awt.Point[x=10,y=200]

You may notice the arrows - this is how to draw a reference; the object refers to the memory with the value in it, so we draw an arrow that points to the memory that was allocated with new. In many cases you will not be concerned with this level of detail, especially once you learn how references work, and will not draw the arrows.

Often you will not draw the references for String objects. However, don't forget that they are references, because String is a class.

What about primitives, are they references? ... No they are not. Since primitives are not references do not use new with primitive data types; your code will not compile.

If you get any error messages complaining about NullPointerException in your code, you should draw your references. This error message means that you have a reference that is set to null, which means the object doesn't have any memory, and your code is trying to use it before allocating the memory with new. Drawing the references as you hand-simulate your code will help you fix this error in your program.

You may have also noticed that println() prints out the point, even though a Point is not a String and we don't convert p1 to a string. This is done automatically by println() with a call to the method toString(). This method takes no arguments and returns a String representation of the object. How would you declare an object called str1 and set it to a string representation of the current value of p1? Hint - use dot-notation and the method toString().

Remember that since an object retains its value until it is changed, and p1 and str1 are different objects, if you change p1 after setting str1, the object str1 will not change unless you write the code to give it a new value. This rule applies to all objects, even primitives, so it may be more clear if we look at a code example using two doubles. Read the comments - they explain the concept. You may want to practice your drawing with this code.

Changing Objects

	double	x;
	double	y;

	x = 19.3;	// x now has the value 19.3
	y = x;		// y is set to 19.3, the current value of x
	x = 24.0;	// x is now set to 24.0

	// since y is a separate object, x is 24 and y it is still 19.3.