All the rar files have the same 
password : http://learning4you.blogspot.com/

Sunday, November 9, 2008

Creating New Objects in java

Creating New Objects

When you write a Java program, you define a set of classes. "Object-Oriented Programming and Java," classes are templates for objects; for the most part, you merely use the class to create instances and then work with those instances. In this section, therefore, you'll learn how to create a new object from any given class.

Remember strings from yesterday? You learned that using a string literal-a series of characters enclosed in double-quotes-creates a new instance of the class String with the value of that string.

The String class is unusual in that respect-although it's a class, there's an easy way to create instances of that class using a literal. The other classes don't have that shortcut; to create instances of those classes you have to do so explicitly by using the new operator.

Note
What about the literals for numbers and characters? Don't they create objects, too? Actually, they don't. The primitive data types for numbers and characters create numbers and characters, but for efficiency, they aren't actually objects. You can put object wrappers around them if you need to treat them like objects (you'll learn how to do this in "Casting and `Converting Objects and Primitive Types").

Using new

To create a new object, you use the new operator with the name of the class you want to create an instance of, then parentheses after that. The following examples create new instances of the classes String, Random, and Motorcycle, and store those new instances in variables of the appropriate types:
String str = new String();

Random r = new Random();

Motorcycle m2 = new Motorcycle();
The parentheses are important; don't leave them off. The parentheses can be empty (as in these examples), in which case the most simple, basic object is created; or the parentheses can contain arguments that determine the initial values of instance variables or other initial qualities of that object:
Date dt = new Date(90, 4, 1, 4, 30);

Point pt = new Point(0,0);
The number and type of arguments you can use inside the parentheses with new are defined by the class itself using a special method called a constructor (you'll learn more about constructors later today). If you try and create a new instance of a class with the wrong number or type of arguments (or if you give it no arguments and it needs some), then you'll get an error when you try to compile your Java program.

Here's an example of creating several different types of objects using different numbers and types of arguments. The Date class, part of the java.util package, creates objects that represent the current date. Listing 4.1 is a Java program that shows three different ways of creating a Date object using new.


Listing 4.1. Laura's Date program.
 1: import java.util.Date;
2:
3: class CreateDates {
4:
5: public static void main(String args[]) {
6: Date d1, d2, d3;
7:
8: d1 = new Date();
9: System.out.println("Date 1: " + d1);
10:
11: d2 = new Date(71, 7, 1, 7, 30);
12: System.out.println("Date 2: " + d2);
13:
14: d3 = new Date("April 3 1993 3:24 PM");
15: System.out.println("Date 3: " + d3);
16: }
17: }

Date 1: Tue Feb 13 09:36:56 PST 1996
Date 2: Sun Aug 01 07:30:00 PDT 1971
Date 3: Sat Apr 03 15:24:00 PST 1993

Analysis
In this example, three different date objects are created using different arguments to the class listed after new. The first instance (line 8) uses new Date() with no arguments, which creates a Date object for today's date (the first line of the output shows a sample; your output will, of course, read the current date and time for you).

The second Date object you create in this example has five integer arguments. The arguments represent a date: year, month, day, hours, and minutes. And, as the output shows, this creates a Date object for that particular date: Sunday, August 1, 1971, at 7:30 a.m.

Note
Java numbers months starting from 0. So although you might expect the seventh month to be July, month 7 in Java is indeed August.

The third version of Date takes one argument, a string, representing the date as a text string. When the Date object is created, that string is parsed, and a Date object with that date and time is created (see the third line of output). The date string can take many different formats; see the API documentation for the Date class (part of the java.util package) for information about what strings you can use.

What new Does

When you use the new operator, the new instance of the given class is created, and memory is allocated for it. In addition (and most importantly), a special method defined in the given class is called to initialize the object and set up any initial values it needs. This special method is called a constructor. Constructors are special methods, defined in classes, that create and initialize new instances of classes.
New Term
Constructors are special methods that initialize a new object, set its variables, create any other objects that object needs, and generally perform any other operations the object needs to initialize itself.

Multiple constructor definitions in a class can each have a different number or type of arguments-then, when you use new, you can specify different arguments in the argument list, and the right constructor for those arguments will be called. That's how each of those different versions of new that you used in the CreateDates class can create different Date objects.

When you create your own classes, you can define as many constructors as you need to implement that class's behavior. , "More About Methods."

A Note on Memory Management

Memory management in Java is dynamic and automatic. When you create a new object in Java, Java automatically allocates the right amount of memory for that object in the heap. You don't have to allocate any memory for any objects explicitly; Java does it for you.

What happens when you're finished with that object? How do you de-allocate the memory that object uses? The answer, again, is that memory management is automatic. Once you're done with an object, you reassign all the variables that might hold that object and remove it from any arrays, thereby making the object unusable. Java has a "garbage collector" that looks for unused objects and reclaims the memory that those objects are using. You don't have to do any explicit freeing of memory; you just have to make sure you're not still holding onto an object you want to get rid of. You'll learn more specific details about the Java garbage collector

New Term
A garbage collector is a special thing built into the Java environment that looks for unused objects. If it finds any, it automatically removes those objects and frees the memory those objects were using.

0 comments:

Related Posts with Thumbnails