2005 Pearson Education, Inc.  All rights reserved.
1
Introduction toClasses and Objects
 2005 Pearson Education, Inc.  All rights reserved.
2
Classes, Objects, Methods and InstanceVariables
Class provides one or more methods
     - Method represents task in a program
Classes contain one or more attributes
Specified by instance variables
Carried with the object as it is used
 2005 Pearson Education, Inc.  All rights reserved.
3
Declaring a Class with a Method andInstantiating an Object of a Class
Each class declaration that begins with keywordpublic must be stored in a file that has the samename as the class and ends with the .java file-name extension.
 2005 Pearson Education, Inc.  All rights reserved.
4
Common Programming Error
Declaring more than one public class inthe same file is a compilation error.
 2005 Pearson Education,Inc.  All rights reserved.
5
 2005 Pearson Education, Inc.  All rights reserved.
6
Class GradeBookTest
Java is extensible
Programmers can create new classes
Class instance creation expression
Keyword new
Then name of class to create and parentheses
Calling a method
Object name, then dot separator (.)
Then method name and parentheses
 2005 Pearson Education,Inc.  All rights reserved.
7
 2005 Pearson Education, Inc.  All rights reserved.
8
 Declaring a Method with a Parameter
Scanner methods
nextLine reads next line of input
next reads next word of input
 2005 Pearson Education,Inc.  All rights reserved.
9
 2005 Pearson Education,Inc.  All rights reserved.
10
//  GradeBookTest.java
import java.util.Scanner;
public class GradeBookTest
{
       public static void main( String args[] )
      {
         Scanner input = new Scanner( System.in );
        GradeBook myGradeBook = new GradeBook();
        System.out.println( "Please enter the course name:" );
           String nameOfCourse = input.nextLine(); // read a line of text
           System.out.println();
           myGradeBook.displayMessage( nameOfCourse );
   }
}
 2005 Pearson Education, Inc.  All rights reserved.
11
Instance Variables, set Methods and getMethods
Variables declared in the body of method
Called local variables
Can only be used within that method
Variables declared in a class declaration
Called fields or instance variables
Each object of the class has a separate instance ofthe variable
 2005 Pearson Education,Inc.  All rights reserved.
12
public class GradeBook
{
  private String courseName;
 
  public void setCourseName( String name )
  {
    courseName = name;
  }
  public String getCourseName()
     {
    return courseName;
  }
  public void displayMessage()
  {
System.out.println( "Welcome to the grade book for\n“ + getCourseName() );
  }
}
 2005 Pearson Education, Inc.  All rights reserved.
13
Access Modifiers public and private
private keyword
Used for most instance variables
private variables and methods are accessible onlyto methods of the class in which they are declared
Declaring instance variables private is known asdata hiding
When a program creates (instantiates )an object ofclass GradeBook variable courseName isencapsulated (hidden) in the object and can beaccessed only by methods of the objects class
 2005 Pearson Education, Inc.  All rights reserved.
14
GradeBookTest Class ThatDemonstrates Class GradeBook
Default initial value
Provided for all fields not initialized
Equal to null for Strings
 2005 Pearson Education, Inc.  All rights reserved.
15
set and get methods
private instance variables
Cannot be accessed directly by clients of the object
Use set methods to alter the value
Use get methods to retrieve the value
 2005 Pearson Education,Inc.  All rights reserved.
16
 2005 Pearson Education,Inc.  All rights reserved.
17
 2005 Pearson Education, Inc.  All rights reserved.
18
Initializing Objects with Constructors
Constructors
Initialize an object of a class
Java requires a constructor for every class
Java will provide a default no-argument constructorif none is provided
Called when keyword new is followed by the classname and parentheses
When you declare a class you can provide your ownconstructor to specify custom initialization forobjects of your class
 2005 Pearson Education,Inc.  All rights reserved.
19
 2005 Pearson Education,Inc.  All rights reserved.
20
 2005 Pearson Education,Inc.  All rights reserved.
21
Call constructor to create first gradebook object
Create second grade book object
 2005 Pearson Education, Inc.  All rights reserved.
22
Displaying Text in a Dialog Box
Windows and dialog boxes
Many Java applications use these to display output
JOptionPane provides prepackaged dialog boxescalled message dialogs
 2005 Pearson Education,Inc.  All rights reserved.
23
Outline
Dialog1.java
 2005 Pearson Education, Inc.  All rights reserved.
24
Displaying Text in a Dialog Box
Package javax.swing
Contains classes to help create graphical userinterfaces (GUIs)
Contains class JOptionPane
Declares static method showMessageDialog fordisplaying a message dialog
 2005 Pearson Education, Inc.  All rights reserved.
25
Entering Text in a Dialog Box
Input dialog
Allows user to input information
Created using method showInputDialog fromclass JOptionPane
 2005 Pearson Education,Inc.  All rights reserved.
26