Professional Documents
Culture Documents
2
Development lifecycle
3
Class file?
4
How a JVM works?
5
Classes and Objects
- An object has both a state and behavior. The state
defines the object, and the behavior defines what the
object does.
Class Name:
A class template
Circle
Data fields:
radius
Methods:
getArea
7
Classes
class Circle {
/** The radius of this circle */ Data field
double radius = 1.0;
/** Construct a circle object */ Constructors
Circle() { }
/** Construct a circle object */
Circle(double newRadius) {
radius = newRadius;
}
/** Return the area of this circle */ Method
double getArea() {
return radius * radius * 3.14159;
}
}
8
Know Your Data item
- Fields define the state; local variables don’t
class Car {
String licensePlate = ""; // field
double speed = 0.0; // field
static double maxSpeed; = 123.45;// fields
boolean isSpeeding(int i) {// parameters
double excess; // local variable
excess = maxSpeed - this.speed;
if (excess < 0) return true;
else return false;
}
}
9
Constructors
class Circle {
…….
Circle() { }
10
Constructors
11
Declaring Object Reference Variables
ClassName objectName;
- Example:
Circle myCircle;
12
Accessing Objects
- e.g., myCircle.radius
- e.g., myCircle.getArea()
13
Using “this”
class Circle {
…....
Circle(double newRadius) {
this.radius = newRadius;
}
…..
}
14
The null Value
15
Copying Variables of Primitive Data Types
16
Copying Variables of Object Types
17
Garbage Collection
18
Array of Objects
19
Visibility Modifiers
- By default, the class, variable, or method can be
accessed by any class in the same package.
- public: The class, data, or method is visible to any
class in any package.
- private: The data or methods can be accessed only by
the declaring class.
- The get and set methods are used to read and modify
private properties.
- Rule #1:
Data Fields should always be private.
Always, Hide Information
20
Accessibility of Methods
1. public: visible everywhere
2. protected: visible in the package and in subclasses
3. Default (no modifier): visible in the package
4. private: visible only inside the class (more on this later)
- A method can access fields and methods that are
visible to it.
• Public method and fields of any class
• Protected fields and methods of superclasses and
classes in the same package
• Fields and methods without modifiers of classes in the
same packages
• Private fields and methods of the same class.
21
Accessibility of Methods
22
Accessibility of Methods
23
Types of Methods
- Accessor methods:
public String getName()
{ return name; }
- Mutator methods:
Public void setSalary(double newSalary)
{ salary = newSalary;}
24
Static Methods
- Usage:
className.staticMethod()
NOT objectName.staticMethod()
25
Static Methods
class Employee{
public static int getNumOfEmployees()
{
return numOfEmpolyees;
}
private static int numOfEmployees = 0;
…
}
//to invoke it:
Employee.getNumOfEmployee(); // ok
Harry.getNumOfEmployee(); // not this one
26
Static Methods
class EmployeeTest
{
public static void main(String[] args)
{… }
}
27
Conclusions
28