Skip navigation

Lu's Notes

down to bottom of page

Java Object and Class Notes

Warning: These notes have been collected from several different sources - each uses different terminology... they may even be contradictory...

Java is object oriented programming.

An object is a unique instance of a class.
A class is a description of a possible object.
Or you could say that a class is a plan for an object and an object is what results when the plan has been carried out.
     Example  class: a house plan      objects: the houses built from the house plan
     Example  class: a cookie cutter  objects: the cookies created with the cookie cutter
The programmer defines classes that describe future objects when the program is written. As the program runs, objects are created and their methods are activated. The program does its work by creating objects and activating their methods.

A class is like a cookie cutter that can be used many times to make many cookies. There is only one cookie cutter, but can be used to make many cookies. Cookies are objects and each one has its own individuality because each one is made out of a different section of dough. Different cookies may have different characteristics, even though they follow the same basic pattern. For example, different cookies can have different candy sprinkles or frosting (values/variables). (class: CookieCutter), (object: Cookie), (variables: sprinkles, frosting)

Classes are mostly used to create objects, just as a cookie cutter is mostly used to create cookies. New Cookies can be created. Cookies can be destroyed. But destroying cookies does not affect the cookie cutter. It can be used again if there is enough cookie dough left. A class has an existence, just as a cookie cutter has an existence. A class and its objects are different types of things, just as a cookie cutter and its cookies are different types of things. A cookie cutter has characteristics that are not shared with cookies. For example, a cookie cutter is made of steel and has sharp edges. After a cookie cutter has been used for a while, there will be many cookies (soft, rounded, doughy things) but still only one thing made of steel with sharp edges. In the Java language, characteristics of a class definition (but not of its objects) are called static. There is only one class definition for a given class, so when a program is running, if something is static then there is only one of it.

A challenge in object-oriented development is finding the right objects (nouns) to model the system being represented. Identifying objects in a problem domain is an art, not a science.

 What makes an "object" ?
An object is made of tangible material (a pen is made of plastic, metal, ink)
An object may also be an intangible thing, a concept (such as a bank account.)
An object holds together as a single whole (the whole pen, not a fog.)
An object has properties (the color of the pen, where it is, how thick it writes...)
An object can do things and can have things done to it.
An object is usually a "noun", a thing, real or imaginary, simple or complex...
   (bicycle, receipt, complex number, ball, satellite dish, person, employee)
An object has identity (it acts as a single whole, is an individual thing, it has it's own variables )
An object has state (it has various properties, it has data, which might change... the values held in it's variables)
An object has behavior (it can do things and can have things done to it, it has methods, often using those variables)
A good object has both:  attributes/properties/descriptors/variables
   and  actions(a verb)/operations/responsibilities/behaviors/methods

Some nouns are merely variations of other nouns, they might express actions or events, or are attributes like color. Test objects to determine their validity:
1. Are they relevant to the problem domain, the main purpose of the system/program?
2. Do they have both: attributes/variables  and  actions/behaviors/methods?
3. Do they have independent existence? Do they make sense by themselves? Do they need to exist independently, rather than being an attribute or action of another object? Can they exist without any other objects?

A computer/software "object" consists of both variables (state information) and methods (recipes for behavior.) Both of these are called "members" of the object. This object has some variables (color, size, and location) that describe it and has some methods (move, resize, set and get) that control its behavior. First the object must be created. Creating an object is called instantiation. Then object "does things" by running it's methods. All method names will have "( )" at their end. When a Java application is being executed (as the program is running), the objects are instantiated (are created) and their methods are invoked (are run.)

The two types of things inside of an object---variables and methods---are sometimes called members of that object. The members of an object are accessed using dot notation. ( referenceToAnObject . partOfTheObject ) The example below creates a String object, referred to by the variable cookie1. The length( ) method is a member of cookie1. To refer to this method of the object cookie1 put the two together with a dot: len = cookie1.length( );
     1.The expression on the right of the "=" is evaluated.
     2.The resulting value is stored in the variable on the left of the "=" sign.
The right-hand side of this particular assignment statement executes the method length( ) which is member of cookie1. Executing this member method will return the number of characters in the object. This dot notation is also known as a method call. All method names will be followed by parentheses, as in length( ).

To create an object, there needs to be a description/plan of the possible object (or objects.) 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. The class does not by itself create any objects. A class is a description of potential objects and can be used many times to make many objects of the same type. The objects do not have to have the same data inside of them, just the same over all plan. Every object in Java is an instance of a class. The class definition has to exist first before an object can be created/constructed.

Java has many data types built into it, and you (as a programmer) can create as many more as you want. However, all data in Java falls into one of two categories: objects and primitive data. The only type of data a programmer can define is an object data type (a class). Every object in Java is an instance of a class. The class definition has to exist first before an object can be constructed. Any data type you invent will be a type of object. There are only the eight primitive data types. A (crude) analogy is that a primitive data value is like a nut or a bolt, but an object is like a whole machine.

Since Java is a object oriented programming language...
 Many classes are already defined in the Java Development Kit.
 A programmer can create new classes to meet the particular needs of a program.
 The data type of an object is called it's class.
 An object usually consists of many internal pieces.
 An object is a big block of data. An object may use many bytes of memory.
 A programmer can not create new primitive data types.
 A primitive data value uses a small, fixed number of bytes.
 There are only eight primitive data types.

An object is a section of memory (with variables and methods), and a class is a description of a possible object. When an object is created (instantiated) the description is followed. One way in which objects are created is when the new operator is used with a constructor. A constructor has the same name as the class. The new operator says to create a new object. It is followed by the name of a constructor. The constructor String( ) is part of the definition for the class String. Constructors often are used with values (called parameters) that are to be stored in the data part of the object that is created. There are usually several different constructors in a class, each with different parameters. Sometimes one parameter is more convenient to use than another, depending on how the new object's data is to be initialized. However, all the constructors of a class follow the same plan in creating an object.

An object reference variable can exist without referring to an object.
A reference variable can be declared without initialization:  String myString;
Also, a reference can be set to null:  String b = null;
    * Remember:  String a = "";  is NOT the same as:  String b = null;
       the first is like having a blank sheet of paper, versus having no paper at all (an empty string.)
An object can exist without an object reference variable referring to it.
As in the  String myString; example. Such objects are temporary.

See Java Links for more info, Object & Classes , Data Types , Operators , Conditionals , Definitions , Keywords , File Structure , Code Examples , Class Skeleton , Java Notes

up to top of page   Return to Top of Page   up to top of page up to top of page