CSE 115 - Spring 2008 - Banner
   CSE 115 - Spring 2008 - Introduction to Computer Science for Majors I
CSE 115 - Spring 2008 - Navigation CSE 115 - Spring 2008 - Tutorials: Objects B3

Object oriented program is made up of objects. These are entities which posses attributes and capabilities .Attributes describe the object and the capabilities explain what things or tasks (actions) an object can perform.

Process Going On Inside The Machine While Creating Objects In Java:

Java follows a mechanical process while creating an object,the process is called edit-compile-run process. This process includes the following steps:

1. Creation of your source code.

2. Compilation of code to create a byte code. (After this step if syntax errors occur then return to editor to fix them.)

3. Run your program using applet viewer or java interpreter. (After this step if runtime errors occur then return to your editor to fix them.)

Editing: A program in high level programming languages is written using the instructions in the form of programming language statements using text editor and the resulting documents is called source code.

Compiling: The source code file will initially have .java extension.Then it is compiled using a compiler software called JavaC.This compiler converts the source code  into bytecode .Now the file have the extension of .class. A compiler will successfully compile the file only if it has no syntax  errors .If it has any of them then you have to keep editing till compiler shows no error.Even after the successful  compilation it may have runtime or logic errors.

Running:The byte code file is then to be run using another software called JVM(Java  Virtual Machine).After running it make sure that result produced is your desired one or make changes in the source code and then compile and run. The runtime and logic errors can be discovered after running process.If any of them are found you have to correct those errors and then repeat the same process.

Mechanism of Creating Objects In Java:

Using Java one can create objects .Objects are created by creating an instance of a class. Here is an example of it.

 

                  Date tomorrow = new Date( );

 

Above statement creates a new Date object .

Above Statement performs three tasks: declaration, instantiation, and initialization. Date tomorrow is a variable declaration which declares to the compiler that the name tomorrow will be used to refer an object whose type is Date The  new operator instantiates the Date tomorrow and Date initializes the object .

 

Declaring the Object:

Declaration of object is not very important part of object creation,although it appears on the same line. Like other variable declaration. It can appear alone like this:

 

Date tomorrow;

 

Declaring variable to hold an object is just like declaring a variable to hold a value of primitive type:

 

type name

 

type is the data type of the object and name is the name to be used for the object. Classes and interfaces are same in Java like data types. So,type can be the name of class or the name of interface. Classes and interfaces both are reference types. A reference may also be called an object reference or an array reference,depending on the data to which reference refers. Declarations guides compilers that it you will be using name to refer to a variable whose type is type. Declaration do not create new objects. Only Date tomorrow will not be able to create new object,just the variable tomorrow to hold a Date object,To create new object,use the new operator.

Instantiating an Object:

The new operator instantiates a class by allocating memory for a new object of that type,new requires a single,postfix argument: a call to a constructor. Each one of the Java class provides a set of constructor used to initialize new objects of that type. The new operator creates the object, and the constructor initializes it.

Example:

 

               new Square (200,200) ;

 

Square (200,200) is the argument to new .The new operator returns a reference to he newly created object. This reference can be assigned to a variable of the appropriate type.

Example:

 

              Square squa = new Square (200,200);

 

After this squa refers to a Square object whose origin is at (0,0), and length is 200

 

Initializing Object:

Initialization  of the object is done using the constructor methods.Class provides various types of the constructor  methods that can be used for the purpose.It is easy to recognize the constructors as their names are same as that of the classes .Also they do not have any return type.A constructor may or may not arguments. Java automatically assigns  a constructor with no arguments if a class does not explicitly define a constructor .

There can be just one constructor or multiple constructors in a class.If there are multiple constructors then they have a same name but they have different number or type of arguments. A new object will be initialized by every constructor in a different way. A constructor without any arguments is  called a default constructor.See the example given below.

 

              Date( )

 

Important definitions of the some basic terminologies in Java:

1. Class : A class is a model  using which objects are created.

2. Object : An object is an entity  having properties(that describes the object) and capabilities(actions that an object performs).

3. Instantiation: Instantiation is a process in which instances of  different classes.
4. Keyword: Keyword is a word which holds a special meaning in that language.Examples of keywords in

                   Java are: public, class, package, etc.

                  

5. Expression: Expression is a series of characters which when executed(evaluated ) returns a value.
6. Statement: A statement can be said of as the building block of a language. Statement can have inner

                       components .                                     

 

 

CSE 115 - Spring 2008 - Footer

 

 
Page Maintained by: Adrienne Decker