Home : Course Map : Chapter 3 : Java :
Demo 1: Creating Objects
JavaTech
Course Map
Chapter 3

Introduction
Class Definition
Data Fields
Methods
Constructors
Instantiation
  
Demo 1
  Demo 2
Static Members
  Demo 3

Value&Reference
  Demo 4
Overloading
   Demo 5
Wrappers 
  Demo 6
Autobox/Unbox
   Demo 7
Arrays
  Demo 8
Exceptions
Exercises

    Supplements
MoreAboutObjects
Creating Types

Classes,Objs&JVM

Java OOP vs C++
Garbage Collection
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The demos below illustrate how to create a class object. This can also by referred to as instantiating (creating an instance of) a class.

For the demos here and in the rest of this chapter, we will use the the applet starter StartApplet2.java and the application starter StartApp2.java. Each includes the class Test for inserting methods and data fields. In the main routine, which is where the processing begins, we create an instance of the Test class and invoke its methods, access its data, etc..

The first applet - DataFieldApplet - demonstrates a class with only a data field. In the init() method we create an instance of class Test and then use the reference "test" and the "." operator to access the "i" variable in the object:

DataFieldApplet.java
(Output goes to browser's Java console.)

public class DataFieldApplet extends java.applet.Applet
{
  public void init ()
  {
     //Create an instance of the Test class
     Test test = new Test();

     // Access the variable i in the Test object.
     int j = test.i;
     System.out.println ("test.i = "+j);
     System.out.println ();

  }

  // Paint message in Applet window.
  public void paint (java.awt.Graphics g)
  {
g.drawString ("DataFieldApplet", 20, 20); }
}

// Use Test to examine various aspects of class design
class Test
{

    int i = 5;

}

Application version:
DataFieldApp.java

 

The second applet - MethodApplet - demonstrates a class with both a data field and a method. In the init() method we create an instance of class Test and then use the reference "test" and the "." operator to access the "aMethod(int j)" method in the object:

MethodApplet.java
(Output goes to browser's Java console.)

public class MethodApplet extends java.applet.Applet
{
  public void init () {
     //Create an instance of the Test class
     Test test = new Test ();

     // Invoke a method
     test.aMethod (3);
  }

  // Paint message in Applet window.
  public void paint (java.awt.Graphics g)
  {
g.drawString ("MethodApplet", 10, 20); }
}

// Use Test to examine various aspects of class design
class Test
{

    int i;
    void aMethod (int j) {
      i = j;
      System.out.println ("In aMethod()");
      System.out.println ("i = "+i);
      System.out.println ();
    }

}

Application version:
MethodApp.java

 

Latest update: Oct. 19, 2004

           Tech
OOP in Tech Apps
Complex Number
Histogram Class
  Demo
More Wrappers

           Physics
OOP in Physics
Particle Class
Root Finding
  Demo 1
Newton Methods
  Demo 2
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.