CS1335 Java and Processing
  • CS 1335 Computer Science 1
  • Getting Started
    • Processing IDE
    • Java vs Javascript
    • Review: Processing, Functions
    • HSB Color Mode
      • HSB Color Wheel
        • Example Code
      • HSB Color Palette Tool
    • Recursion
      • Recursion Call-Stack
      • Example Code
        • Example Code Feb 5 S20
        • Feb 12 Code
  • Project 1
    • Subjective Modeling of Emotions
    • Emotions represented using color, form, space
      • Kandinsky Color - Emotion
      • Emotional Intelligence
    • Project 1: PShapes
      • Example Code
      • Inspiration
    • PShape with Cutout - Inner Contour
    • VertexShape - Recursion
    • Project 1: Recursive Drawing
    • Project 1: Programmatic Variations in Color
      • Recursion with rotate, scale
      • Plan Region Size, Color
    • Map Function
    • Transforms for Mirroring
    • Project1-Steps
  • Grid Based Designs
    • Computational Design
      • Generative Design
    • Artist: Victor Vasarely
    • Grid Pattern Design
    • 1D - Array of PShapes for Grid Layout
      • Truchet Tiling
      • Example Code
    • PShapes in Grid Regions
    • Grid Region Logic
    • Pattern Preview - Transforms: Translate & Scale
  • Project 2
    • Project 2 - 2D Arrays for Gradient Logic
      • 2D Array Grid with Labels
    • Grid Patterns using 2D Array Indexes: i, j
      • Example Class Code
    • lerpColor( ) and map( ) Functions
    • Demo Lerp Colors
    • 2D Arrays with lerpColor
    • Create PShape 2D Array
    • Function: Populate2DArray( )
    • Function: DisplayShapeMatrix()
    • Transforms for Position, Rotation, Scale of ShapeMatrix Elements
    • Project 2 - Steps
    • Animation for ShapeMatrix
      • Animation w/Noise
  • Object Oriented Programming
    • Introduction to Objects
    • OOP vs Data-Flow
    • Button States
    • Buttons as Objects
      • Button Class
    • Create Object Instances
    • Button Types
    • Modeling Buttons: States and Events
    • OOP - Inheritance
    • OOP - Polymorphism
    • Child-Class: PImageButton
    • PShape - SVG Objects
    • Menu of Buttons
    • ButtonGroup - Final Version
    • Slider Controller
    • UML Class Diagram
  • Project 3
    • Project 3 - Logic, Steps
    • Example Code S20
      • Code Wed Apr 1
      • Code Wed Apr 8 v1
      • Code Wed Apr 8 v2
      • Code Mon Apr 13
      • Code Wed Apr 15
      • Code Mon Apr 20
      • Code Wed Apr 22
      • Code Mon Apr 27
      • Code Wed Apr 29
    • Project 3 - Class Definitions
      • Button
      • PImageButton
      • ButtonGroup
      • Pattern
        • PShapes - SVG, Vertex Shapes
        • Setting Colors For Patterns
        • Pattern - With Child-PShapes
      • Slider
      • Particles
  • Java Syntax
    • Java Syntax
      • Typed-Variables
      • Float - Integer Conversion Errors
      • Modulus
      • Functions
      • Object Reference Data Types
      • Arrays
        • Class Example Code
      • Switch-Case Statement
      • Ternary Operator
      • Class
      • Learning Science
    • UML Class Diagram
    • Glossary
  • Resources and References
    • Resources
    • Random Inspiration
      • Ulm School
      • Heart-Mind, Mind, Body
      • Statistical Uncertainty
Powered by GitBook
On this page
  • Class Definition
  • Create Objects: Reference-Type Variable

Was this helpful?

  1. Object Oriented Programming

Create Object Instances

Class Definition

To create an instance of an object, first we need to have the full definition of the class, which includes 3 main components: Instance Variables, Constructors, and Methods.

Create Objects: Reference-Type Variable

Once we have the class definition, such as our Button class, then in order to create an instance of a class object, we do several things:

1. Create a Reference-Type variable: We need to declare that we will make an object of that specific data-type, so we must create a reference variable that we can use to access (or refer) to our object instance.

  • Button myButton; //declare a reference-type variable, this can point to a Button object (it can hold the memory address of a Button-type object);

  • The default value for a reference-type variable is 'null'.

  • null keyword is a literal value that means an reference-type variable does not currently refer to an object instance. This allows us to test whether or not a reference-variable currently contains a valid memory address of an object.

  • Button myButton; when we first declare an object reference, it is null;

  • myButton = null; Allows us explicitly set an object reference to null

2. Call a valid Constructor Method (function) to create an object instance:

  • We need to use the keyword new to call a constructor

  • we need to call one of the constructors we defined in the class.

  • Executing: new Button() does several things:

    • it creates memory for our object in the 'Heap', the memory place where objects are stored for our program.

    • it executes code in the constructor method, this is a good place to initialize variables for our object

  • We will usually call a constructor using the following syntax:

    • myButton = new Button() - this sets the value of our object reference variable to be the memory address location in the heap where our object data is stored.

3. Use the Object Instance: once we have an object reference variable which is pointing to an object instance, the memory location of our object's data , now we can call methods of the object using dot notation: myButton.Click(); or we can modify instance variables myButton.x = 5;

4. Null pointer exception: If we try to do step 3 before we've done both step 1 and 2, we try to use an reference variable to call object methods,....before we have established a connection between our reference variable and an object instance....then we'll get a null pointer Exception.

5. Multiple references to a single object: It is perfectly ok to have more than 1 object reference variable pointing to an object instance. This would look like:

Button myButton = new Button();  //create reference variable and call constructor 
Button myButtonPointer = myButton;  // 2 Button reference variables are both `referring` to a single Button object instance.

6. Passing Objects into Functions as Parameters

When we call a function or a method, if the function has an input parameter which is an object: then the address of the object is passed into the function. So, changes made to the function's local-parameter reference-variable are actually affecting the object, since it points to the same object adddress.

//create function that takes an object as input parameter
void myFunction( Button localButton ){
     localButton.x = 100;
}
Button myButton = new Button();
myButton.x = 5;
myFunction( myButton ); 

println(myButton.x);  ///myButton.x is now 100, it was modified in the function

When this function completes executing, then the local parameter, localButton, is destroyed, so it's no longer pointing at the object.

PreviousButton ClassNextButton Types

Last updated 5 years ago

Was this helpful?