Review of Objects - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Review of Objects

Description:

Computer Memory. Memory 'space' Your program. main ... Computer Memory. Memory 'space' The Java class libraries. JOptionPane, JFrame, Random, Graphics, ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 20
Provided by: msresearch
Category:
Tags: objects | review

less

Transcript and Presenter's Notes

Title: Review of Objects


1
Review of Objects
2
  • // header
  • public class Lab1MainClass
  • public int y
  • public static void main(String args )
  • int x
  • supportMethod( )
  • // end main method
  • public static supportMethod( )
  • // end supportMethod
  • // end Lab1MainClass

class definition shared variables main
method not-shared method call support
method note end brackets
3
Computer Memory
Memory space
Your program main
support method
4
  • // header
  • import java.util.Random
  • public class Lab1MainClass
  • public int y
  • public Random myGen
  • public static void main(String args )
  • int x
  • myGen new Random()
  • // end Lab1MainClass

library shared variables
? ?
5
Instantiation
  • You or someone else writes a class
  • public class someClass
  • ..
  • You use the class in your program
  • You must make a local copy
  • someClass myObject new someClass( )
  • Or, two-step
  • public someClass myObject
  • myObject new someClass()

6
What is a class?
  • A program, with no main
  • Contains useful methods and variables, that you
    might want to use in your program, that HAS a
    main.

7
  • import javax.swing.JOptionPane
  • import javax.swing.JButton
  • public class test
  • public static void main (String args )
  • JOptionPane.showMessageDialog(null, Hello)
  • JButton myButton new JButton( )

8
Computer Memory
Memory space
The Java class libraries
Your program main
JOptionPane, JFrame, Random, Graphics, System,
100s of them
showMessageDialog
new
JButton methods
9
Writing Programs
Memory space
The Java libraries
Your program main
10,000s of classes to choose from
millions of methods
new
objects
10
What if Libraries arent sufficient?
Memory space
The Java libraries
Your program main
10,000s to choose from
millions of methods
Write your own class
new
objects
11
What a Class does
  • A collection of methods and variables.
  • Every Object created from a class, operates as an
    independent, freely running program.
  • Only one main allowed
  • main spawns useable objects, sometimes many
    objects of the same class.

12
What we know about Objects
  • Classes are of no value until
  • They are instantiated

13
Classes
  • Collections of methods and variables
  • You write the classes to do real work

14
Objects
  • They are instantiated from Classes
  • An object, is a Class that has been put to
    work.
  • Since Classes can be used many times in the same
    program, each instance of a Class is a
    separate, usable copy.

15
Copies of Classes
  • Copy is an oversimplification, but the concept
    has run-time value
  • A copy is created using the new statement.
  • A copy of a Class is an Object.
  • non-Static Classes can never be used directly.
  • They must be copied to Objects
  • The copy step is called instantiation.
  • Your program owns and controls the copy.

16
Instantiation
  • Once a class is defined, making it your own for
    use, using the new keyword
  • myClass myObject new myClass( )
  • Creates an instance of the Class.
  • the Class solves a Class of problems.

17
Applications - The main Method
  • The computer runs it, you dont.
  • The computer calls the main method
  • The main method uses methods, contained in
    Objects, derived from Classes that you write.

18
  • public static void main(String args)
  • ShowColors app new ShowColors( )
  • // any method in ShowColors is now useable
  • // as app.MethodName( )

19
The Random class
  • See the demo
Write a Comment
User Comments (0)
About PowerShow.com