C SC 160 Chapter 2: Getting Started with Java (using classes and objects)
major resource: An Introduction to Object-Oriented Programming with Java, fourth
edition,
Wu, McGraw Hill, 2006
[ previous
| schedule
| next ]
Note: most of these topics are also covered in Dr. Pete's No-Nonsense Guide: Using Java Classes
A first program
- Ch2Sample1.java
- note: all Wu source code available in course repository
- main method creates and displays an empty JFrame
- worked through it line by line
- comments
- single line comments using //
- multi-line comments using /* to start and */ to end
- "javadoc" comments using /** to start and */ to end
- javadoc is utility program that reads Java source code and produces HTML documentation
- javadoc recognizes and formats contents of comments starting with /**
- the Java API web pages were produced by javadoc
- import and packages
- package is a group of related classes stored in the same folder
- import statement tells compiler you are going to use classes from specified package
- Example: to import all classes in package java.util, use import java.util.*;
- Example: to import only class Random from package java.util, use import java.util.Random;
- Package hierarchies are stored in corresponding folder hierarchies
- Example: class java.util.Random is stored in file Random.class,
which is stored in folder util, which itself is stored in folder java
- naming conventions
- Class names traditionally start with upper-case letter
- Variable and method names traditionally start with lower-case letter
- Names should be descriptive but cannot contain blanks
- Multi-word names are traditionally composed by mixed case, e.g. timeOfDay
- Names for constants ("final"), traditionally are all upper case with underscore to compose words, e.g. DAYS_IN_WEEK
- declaring reference variable
- "reference variable" means the variable will later identify an object
- We say "reference" because objects, like puppies, intrinsically do not have names. Calling a puppy "Fido" does
not affect the animal itself, it is just our way of referring to the animal.
- Declaration means introduce the identifier and associate it with a class name
- Format: ClassName identifier;
- At this point, it does not yet refer to any objects.
- Example: Random randu;
- creating object
- A class is just a template, to do useful work we must create objects
- Object creation occurs with the expression new ClassName(arguments),
where ClassName is the desired class, and arguments is an
optional list of values for initializing the object
- The thing that actually creates and initializes the object is called the constructor
- The result of the new ClassName(arguments) expression is a reference to the newly-created object
- This reference must be assigned or else it will be lost
- Normally, assign this new reference to the reference variable!
- Example: randu = new Random();, where randu was previously declared by Random randu;
- invoking method with arguments
- Once you have assigned the object reference to a variable, you use this variable to "send messages" to the
object.
- The terminology I normally use instead of "send message", is "invoke method". They are equivalent.
- Format is: objectName.methodName(arguments)
- Again, arguments is an optional list of values needed by the method.
- The method may or may not respond with a result
- If it responds with a result, which we call its return value, the result should be assigned or else it
will be lost (falls into the "bit bucket").
- In either case, the caller suspends execution until the method completes
using JOptionPane for input and output
- find JOptionPane in Java API documentation
- static methods: showConfirmDialog, showInputDialog, showMessageDialog
- A static method is a class method, invoked through the class name, e.g. Math.abs()
- Use showMessageDialog() method to display (output) values
- Use showInputDialog() method to get keyboard input
- We used null as the first argument to both methods, to indicate that the dialog should be centered on the screen
- showMessageDialog is one-way, it does not return a value
- showInputDialog() returns the value entered.
- We studied some example programs
[ C
SC 160 | Peter
Sanderson | Math Sciences server
| Math Sciences home page
| Otterbein ]
Last updated:
Peter Sanderson (PSanderson@otterbein.edu)