Classes and Objects I
- Contents
- Review of Instance Fields and Methods
- Static Class Members
- Static Fields
- Static Methods
- Passing Objects as Arguments
- Returning Objects from Methods
- The
toString
Method - The
equals
Method - Methods that Copy Objects
Review of Instance Fields and Methods
Each instance of a class has its own copy of instance variables.
Example:
- A
Rectangle
class defines alength
and awidth
field. - Each instance of the
Rectangle
class can have different values stored in itslength
andwidth
fields.
- A
Instance methods require that an instance of a class be created in order to be used.
Instance methods typically interact with instance fields or calculate values based on those fields.
Static Class Members
Static fields and static methods do not belong to a single instance of a class.
To invoke a static method or use a static field, the class name, rather than the instance name, is used.
Example: