What's an object?



An object is a way of organizing information. It can be a bundle of data and methods that act on those data.

Suppose that I define an object called "student"


Here is a simple definition of the object student:
public class student {

 public static void main(String arg[]) {
   System.out.println("Hi my name is Fred");
 } // main

} // class student


You can use "edit" at the DOS prompt to write this java application. Save it as "student.java". Compile it with the command "c:\ javac student.java" and run it with "c:\ java student"


Objects can have their own data:

public class student2 {

 public static void main(String arg[]) {

   String name = new String("Fred");

   System.out.println("Hi my name is " + name);

 } // main

} // class student2 

Save this file as "student2.java", compile it with "javac" and run it with "java".



We can send information into an object.

public class student3 {

 public static void main(String args[]) {
 
   String name = new String(args[0]);
   
   System.out.println(name);
   
 } // main

} // class student3


Be careful! this is not a very safe program.
Save it as "student3.java", compile it with "javac" and run it as "java student3 Bill"
(This program expects you to input a name and it has no way to fail gracefully if you don't!!)

A safer version of this program:

public class student4 {

 public static void main(String arg[]) {

   if (arg.length == 0) {
        System.out.println("Use: java student4 name");
   } else {
   String name = new String(arg[0]);
   System.out.println("Hi my name is " + name);
   }
   
 } // main

} // class student4

Try this version, using a name (i.e., "java student4 Bill") and sometimes not.


We can distinguish between the "main" program and an object that it creates.
public class student5 {

  public static void main(String args[]) {
  
  	namer n = new namer();
  
  } // main

} // class student5

class namer {

  namer() {

	String name = new String("Bingo Bobbins");
	System.out.println(name);
  
  } //  constructor namer

} // class namer

In this example we are using a "nested" class and this object is announcing its name to the world.

We can separate the "main" program and the object that it creates.
First, create the object:
public class std {

 std() {
 
  String name = new String("Bilbo Bobbins");
  System.out.println(name);
 
 } // constructor

} // class std
Compile this object: "javac std.java"

Now write the main program that will create a "std" object.
public class student6 {

  public static void main(String args[]) {
  
    std n = new std();

  } // main
  
} // class student6

Run this version with "java student6"

Now we can write a program that makes a bunch of new students and constructs each one with its own name.
First, write the new student object that is constructed with a name:
public class newStudent {

  newStudent(String n) {
  
   System.out.println(n);
  
  } // constructor

} // class newStudent


And the new main class that creates the newStudent object and sends a name to it.
public class student7 {

 public static void main(String[] args) {
 
   newStudent n = new newStudent("Bilbo Bobbins");
 
} // main

} // class student7

Run this as "java student7"


Finally, we can write a program that uses the "newStudent" object and creates a bunch of them.
public class student8 {

  public static void main(String[] args) {
  
   	newStudent a = new newStudent("Alice Apple");
	newStudent b = new newStudent("Betty Buttons");
	newStudent c = new newStudent("Caroline Cotton");
	
	System.out.println("I just created three new student objects");
  
  } // main

} // class student8