lecture-22.ppt

Download Report

Transcript lecture-22.ppt

Classes: user-defined types
Organizing method with a class
• A class is used to organize methods
* Methods that compute Mathematical functions
* The Scanner class
Organizing information with a class
• A class can also be used to organize information
It defines a group of related information that belong to
the same entity
Instance Variables (1)
Java has 4 different kinds of variables
1. Local variables
2. Parameter variables
Instance Variables (2)
3. Class variables
Instance Variables (3)
4. Instance variables
• are defined inside some class
• are defined outside every method of that class.
• are defined without using the keyword static
Creating an object in Java (1)
1. Define a variable to store the location of the object.
1. Create the object
Creating an object in Java (1)
Creating an object in Java (2)
Accessing the information stored
inside an object (1)
Accessing the information stored
inside an object (2)
Accessibility rule for instance variables
• The accessibility of instance variables are controlled
by access modifiers
public = can be accessed in a method inside any class
private = can be accessed in a method inside the same class
Lifetime of Instance variables (1)
Lifetime of Instance variables (2)
These instance variables will cease to exist after the garbage
collection procedure has completed execution
Methods using a object variable as
parameter (1)
• A class in Java is equivalent to a data type
Methods using a object variable as
parameter (2)
• Variable x and variable stu1 will access the same memory
locations
Changing the values of a variable
inside an object