- Objects: state and behavior
- Java Class: describes a kind of object
- Instance: one specific thing of a given kind
- Example: Here the class is "String", the instances are "jimsName" and "somewhere".
String jimsName = "Jim Mahoney"; // a specific instance of String
String somewhere = "Over the rainbow"; // another String object
int x = jimsName.length(); // calling a String method. (behavior)
- Discuss the JimsCar.java and DriveJimsCar.java
programs in this directory.
- Declaring instance variables in classes: public vs private
- Declaring instance methods in classes:
public void doThis( int someNumber){ ... }
- There may be several methods with same name
but different argument lists.
- There usually some special methods to create or
get rid of an instance of an object.
- Typically you define the behavior of an object in
one file, and then create and use those objects from
another file.
- The "new" keyword is used to create an object.
JimsCar redCar = new JimsCar(); // declare and create a JimsCar
- One class can describe many "instances" (objects)
which are members of that class, and share many of same
general properties.
- Each Java class has methods which describe what it
can do. For the String class,
the documentation from Sun is
here, for example.
- Some methods can be run on the class iself,
while others can only be invoked with an instance.
Example:
double x = Math.abs(-3); // Here "Math" is the class itself.
int y = firstString.length(); // While here "firstString" is an instance of the String class.
- These notions will probably take some practice
and exposure before they make a whole lot of sense.
Stay tuned; this is just the first pass.