Lecture Java: Chapter 3 (Using Classes and Objects) presented Using Classes and Objects, Outline, Creating Objects, Creating Objects, Invoking Methods, Reference Assignment, The String Class. | Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William Loftus Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and object references the String class and its methods the Java API class library the Random and Math classes formatting output enumerated types wrapper classes graphical components and containers labels and images Copyright © 2012 Pearson Education, Inc. Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images Copyright © 2012 Pearson Education, Inc. Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference variable String title; No object is created with this declaration An . | Copyright © 2012 Pearson Education, Inc. Chapter 3 Using Classes and Objects Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William Loftus Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and object references the String class and its methods the Java API class library the Random and Math classes formatting output enumerated types wrapper classes graphical components and containers labels and images Copyright © 2012 Pearson Education, Inc. Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images Copyright © 2012 Pearson Education, Inc. Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference variable String title; No object is created with this declaration An object reference variable holds the address of an object The object itself must be created separately Copyright © 2012 Pearson Education, Inc. Creating Objects Generally, we use the new operator to create an object Creating an object is called instantiation An object is an instance of a particular class title = new String ("Java Software Solutions"); This calls the String constructor, which is a special method that sets up the object Copyright © 2012 Pearson Education, Inc. Invoking Methods We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods numChars = () A method may return a value, which can be used in an assignment or expression A method invocation can be thought of as asking an object to perform a service Copyright © 2012 Pearson Education, Inc. References Note that a primitive variable contains the value itself, but an object variable contains the address of the object An object reference can be thought of as a pointer to