Download presentation
Presentation is loading. Please wait.
Published byTiffany Melton Modified over 8 years ago
1
Lecture 9 Using Objects
2
Remember: 3 Different Kinds of Classes 1.Application Class – what we've been doing – Has public static void main ( String [] args) 2.Instantiable Class – use to make objects from – String, Scanner 3.Classes with only static utility methods – like Math class
3
Today—Instantiable Classes Instantiable Class – used to make objects from – SO FAR ONLY: String, Scanner To use an Instantiable class we must – Declare a variable of that class type String message; Scanner myUserInput; – Instantiate a new object message = new String("hello"); myUserInput = new Scanner(System.in); – Use associated methods (name of object '.' method) message.trim(); myUserInput.nextInt();
4
Instantiable Classes make Objects Objects are more complex than primitives – Primitive values are just pieces of data (numbers, characters, or true/false) For example, 2.3 is a value of type double – Instances of classes (objects), on the other hand, contain both data and ways of acting on that data For example, a Point object has an x and y coordinate, but also comes with a way to translate the point by giving the amount of change desired in the x and y coordinates
6
Example: Objects of class PointPoint (download demo file) Point myPoint = new Point(2,-3); //This declares the variable origin, and sets it to (0,0) Point origin = new Point(); //At this point in time, origin is (0,0) //The next line causes origin to change itself to 7,12 origin.translate(7, 12); //The next line causes origin to change itself to 14,24 origin.translate(7, 12); //Prints out: java.awt.Point[x=14,y=24] System.out.println(origin);
8
Review: Primitives int x = 3; double z; z = 3.45;
10
Object (reference) variables
13
Memory Maps – Primitives vs Objects int x = 24 double y = 34.4 Point p; p= new Point(14,35); Point q = new Point(-10,29); (p contains "null")
14
Recap An instance of a class is called an object To make a new object, use the new operator The actions you can invoke on an object are called its methods To use a method, we use dot notation as in the example To find out the details of how to use a particular class, we use the javadoc documentationjavadoc documentation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.