01-11-2012, 01:59 PM
Classes, Objects & Methods
Classes.ppt (Size: 52.5 KB / Downloads: 26)
Class Review
Classes are the design element of Java programs
We cannot declare methods which do not belong to some class
No free methods
We cannot declare variables which do not belong to a class (or a method)
No Global variables
Our programs are the implementation and interaction of things based on these designs
Objects
Objects are the things we use in our programs
This is what has the data and performs the actions
A running program is an interaction between Java objects
The life and times of an Object
Definition
Define the class for this type of object
Declaration
Create a specific name for one of these types of objects
Instantiation
Create the object associated with the name
Set aside memory
Identify associated methods
Use
Destruction
Allocated memory is returned to the system
Instantiation Details
If we don’t instantiate our object,
it will have the special value null
Using a null object is incorrect
When we instantiate an object
We set aside memory for the object’s state information
We identify the methods
Create them if this is first of this object
Refer to them if they already exist
We perform any required initialization and processing
Instance Variables
Variables which belong to the class are called Instance variables
Global through out that class
External access is controlled by visibility modifiers
Each object has it's own copy of instance variables
Except for those declared static
Only one per entire class
Methods
Methods are a Class's Functions
Every method is accessible to any other throughout the same class
External access is controlled by visibility modifiers
All objects of a given class share the same method code
with their own local variables
We must have an object to call a method
Except for those defined to be static
Methods may return a value
Those that do not are declared to be void
Methods may share the same name
Must be distinguished by their signature
Signature
order and number of types of arguments
Defining two (or more) methods with the same name and return type (but different signatures) is called method overloading
Local Variables
Local variables belong to a given method
They are not accessible outside of the method
Local variables are unique to each object
local variables declared to be static will persist between method calls