book musings



missle defense



cultcha blog


scripts & programs

on the town



creds (PDF)



Java Objects

The real-world (meaning the world outside computer programs) is filled with what we could call objects, such as shoes, elephants, pizzas, automobiles. And many of these objects can possess a certain set of behaviors. An elephant can eat peanuts, for instance.

The philosopher Plato once described how all objects in this world were reproductions of some ideal form of that thing, subsequently called the Platonic Form. The chair in your kitchen is a human-made object, based on the ideal idea of a chair.

Objects are also a useful way of thinking about how to write computer programs. This approach called object-oriented programming.

Objects are temporary things. They exist only at run-time. The computer builds them on the fly using something called the class as the blueprint. You could think of class as the Platonic Ideal of an object. In other words, an object is an instance of a class. In Java programming, "Object" and "instance" are interchangeable things.

An object is defined in terms of two things: Its state and its behaviors. The state is what data the object holds. The behaviors are the methods, or the things it can do the data.

To create an object in Java, you first create a class file. Then, within a class file, you make a special type of method, called a constructor.

A constructor is very similar to a standard method, though you do not specify a return type. The constructors appear below the main body of the program, and are called in much the same way as a method.

Here is a program that creates two objects, from a single class:

import java.util.Scanner;

public class elephant {

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.print("Enter how many hundreds of peanuts you'd like the elephant #1 to eat:");

int eat1 = input.nextInt();

System.out.print("Enter how many hundreds of peanuts you'd like the elephant #2 to eat:");

int eat2 = input.nextInt();

//**Called Operators, VARIABLES elephantEat1 and 
//**elephantEat2 are declared to be a TYPE of 

elephantBlueprint elephantEat1;

elephantBlueprint elephantEat2;

//**In the line below an OBJECT is created, given the name //**elephantEat1, which was was defined as a type of //**elephantParts above. 
//**Since it is given a set of values 
//**(in the variable "eat1"), it is an object. It is also, itself, a //**variable

elephantEat1 = new elephantBlueprint(eat1);

System.out.println("Elephant #1 has eaten this many packages of peanuts: " + 

//**Here is another peanut-eating elephant, which is to say //**object, created from the same class, 
//**yet has different data.

elephantEat2 = new elephantBlueprint(eat2);

System.out.println("Elephant #2 has eaten this many packages of peanuts: " + 


//**Here is the Constructor that make the elephant**/

class elephantBlueprint {

int peanutsEatenPounds;

elephantBlueprint(int eat){
peanutsEatenPounds = (eat / 50) ;


Note: The source and a working version of this pogram can be downloaded here.

RANDOM NOTES: Variables can be objects. In the above example, the objects ElephantEat1 and elephantEat2 are variables.

A word on class modifiers: These are the statements that appear before the declaration of a class. "Public" is used if the class can be accessed form elsewhere in the program. Only one "Public" declaration can be made per class, and the name of the class being named public must be the same name as the class file itself.

Material taken (& probably abused) from the book, "Introduction to Java Programming" (7th edition), by Y. Daniel Liang....

All the mistakes, however, are my own...