Java Fundamentals: Using Object Classes and Driver Classes
Java Fundamentals: Using Object Classes and Driver Classes
Java Fundamentals: Using Object Classes and Driver Classes
Overview
This lesson covers the following topics:
Describe the general form of a Java program
Describe the difference between an Object class and a
Driver class
Access a minimum of two Java class APIs
Explain and give examples of Java keywords
Create an Object class
Create a Driver class
Driver Classes
Driver classes:
Contain a main method.
A main method is necessary to run a Java program in
Eclipse.
The main method may include:
Instances of objects from an object class
Variables
Loops, conditional statements (if-else)
Other programming logic
Object Classes
Object classes:
Are classes that define objects to be used in a driver
class.
Can be found in the Java API, or created by you.
Examples: String, BankAccount, Student, Rectangle
The Java API is a library of packages and object classes that
are already written and are available for use in your programs.
Classes
Classes
Classes
Method
Description
charAt(int index)
length()
substring(int beginIndex)
11
Key Terms
Term
Definition
package keyword
Defines where this class lives relative to other classes, and provides
a level of access control.
Use access modifiers (such as public and private) to control access.
import keyword
Defines other classes or groups of classes that you are using in your
class.
The import statement provides the compiler information that identifies
outside classes used within the current class.
class keyword
class variables or
instance fields (often
shortened to fields)
13
The name of the class and the file name must match when the class
is declared public (which is a good practice). However, the keyword
public in front of the class keyword is a modifier and is not required.
Variables, or the data associated with programs (such as integers,
strings, arrays, and references to other objects).
14
Term
Definition
Constructors
Methods
Java Keywords
All Java programs use Java keywords.
Examples include the following words: class, public,
String, int, for, while, and double.
The font color for Keywords will change in the Eclipse
editor.
List of Java keywords:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_
keywords.html
A Java keyword is a word that has a special function in the Java
language, and cannot be used as names for classes, methods,
or variables.
15
16
Camel Case
Camel case is the practice of stringing capitalized words
together with no spaces.
Lower camel case strings capitalized words together
but the lead word is not capitalized. For example:
thisIsLowerCamelCase.
Upper camel case strings capitalized words together,
but the lead word is capitalized. For example:
ThisIsUpperCamelCase.
17
18
19
20
21
Student
Teacher
22
Import Keyword
The import keyword is used to identify packages or object
classes that you want to use in your class.
You can import a single class or an entire package.
You can include multiple import statements.
Import statements follow the package declaration and
precede the class declaration.
import java.util.Scanner;
23
Import Statements
An import statement is not required, and by default, your
class always imports java.lang from the API.
http://docs.oracle.com/javase/7/docs/api/java/lang/Packa
ge.html
You do not need to import classes that are in the same
package as the import statement.
24
25
import java.util.Date;
import java.util.*;
import java.util.Date;
import java.util.Calendar;
26
Access Modifier
27
Class Declaration
Fields/Variables
Constructor
Methods
Access Modifier
29
30
Constructor Methods
A constructor method is unique in Java because:
The method creates an instance of the class.
Constructors always have the same name as the class
and do not declare a return type.
public Student{ }
31
32
33
No parameters
}
34
35
Constructor
37
Constructors
A constructor is a method that creates an object.
In Java, constructors are methods with the same name
as their class used to create an instance of an object.
Constructors are invoked using the new keyword.
Example of code that could be used in a Driver Class to
create an object from the Student constructor:
Student stu = new Student();
38
Main Method
To run a Java program you must define a main method in
a Driver Class.
The main method is automatically called when the class
is called.
Remember to name the file the same as the class.
39
40
41
42
44
45
46
47
48
Code Blocks
A code block is defined by opening and closing curly
braces { }. When examining code blocks, consider the
following:
Every class declaration is enclosed in a code block.
Method declarations, including the main method, are
enclosed in code blocks.
Java fields and methods have block (or class) scope.
public class SayHello
{ // Begin class code block
public static void main(String args[]
{ // Begin method code block
System.out.println(Hello Caron);
} // End method code block
} // End class code block
50
51
Terminology
Key terms used in this lesson included:
Access modifiers
Code blocks
Constants
Constructors
Driver class
import statements
Java API
Java comments
52
Terminology
Key terms used in this lesson included:
Java keywords
Lower camel case
Methods
Object class
Packages
Parameters
Programmer-created class
Upper camel case
Variables
53
Summary
In this lesson, you should have learned how to:
Describe the general form of a Java program
Describe the difference between an Object class and a
Driver class
Access a minimum of two Java class APIs
Explain and give examples of Java keywords
Create an Object class
Create a Driver class
54