Professional Documents
Culture Documents
Understanding Recursion,
Recursion the Static Modifier,
Modifier and Nested Classes
Overview
This lesson covers the following topics:
Create static variables
Use
U static
t ti variables
i bl
Create static methods
Use static methods
Create static classes
Use static classes
Overview
This lesson covers the following topics:
Create linear recursive methods
Create
C t non-linear
li
recursive
i methods
th d
Compare the pros and cons of recursion
Static Modifier
Using instance variables, each instance of a class created
with the keyword new creates a copy of all instance
variables in that class.
For example,
p in the Employee
p y class below, a unique
q copy
py
of lastname and firstname is created for each new
Employee object that is created in a Driver Class.
public class Employee{
private String lastname;
private String firstname;
...more code
}
//create
//c
eate t
two
o Employees
p oyees in a main
a
method:
et od:
Employee e1 = new Employee(Smith, Mary);
Employee e2 = new Employee(Jones, Sally);
4
Static Keyword
Static is a keyword in Java that modifies the association of
an item to a class.
Contents of a class that are identified as static are
shared across all instances of the class.
This means all instances of the class share one copy
py of
the static items, and each have their own unique copies
of instance items, or non-static items.
Static Example
Consider initializing a static String with the value Oracle
called myCompany that represents the employer's
company. Each instance of Employee would still have
their unique instance variables, but would share the static
variable.
e2
e1
Instance Variable
Static Variable
Instance Variable
Mary Smith
Oracle
Sally Jones
Static Variables
Static variables
Are also known as class variables.
Are
A declared
d l d with
ith th
the static
t ti kkeyword.
d
Have only one copy in memory, as opposed to instance
variables which hold one copy per instance
variables,
instance.
Are shared by object instances.
Hold the same value for all class instances.
12
13
14
16
17
fishTank()
() is a static method
and it accesses a static variable
(numTanks).
19
21
22
23
24
Understanding Recursion
A recursive program is one that calls itself one or more
times with each execution until it satisfies the base case
arguments, then discontinues calling itself. It contains:
A base case: A segment of code that tells the program
when to stop calling itself and return a value or void.
A recursive case: A call out to another copy of itself.
A pattern of convergence: The process of working
backward through a problem's data set
set.
25
26
Recursion Process
Recursion looks backward through a chain of events,
while traditional loops look forward through events.
Recursion works backward through convergence on a
base case, where the base case occurs when yyou are
back to the beginning.
28
Forward Thinking:
Process of adding a
number to itself.
Backward Thinking:
Process of subtracting a
number from itself.
Given: t1 = 5
Then: t(n+1)
(
) = tn + 5
Sequence: 5, 10, 15, ...
Given: t1 = 5
Then: tn = t(n-1)
( ) + 5
Sequence: 15, 10, 5
30
32
33
34
Call
Results
recur(4)
3*recur(3)
recur(3)
3*recur(2)
recur(2)
3*recur(1)
recur(1)
35
Call
Results
Actual Value
recur(4)
3*recur(3)
3*(3*(3*1)) = 27
recur(3)
3*recur(2)
3*(3*1) = 9
recur(2)
3*recur(1)
3*1 = 3
recur(1)
37
38
39
fibonacci(4)
fibonacci(3)
40
fibonacci(2)
fibonacci(1) + fibonacci(0)
1
41
fibonacci(2)
fibonacci(1)
fibonacci(1)
fibonacci(0)
1
fib
fibonacci(2)
i(2)
1
+
1
43
44
45
Terminology
Key terms used in this lesson included:
Base case
Class
Cl
method
th d
Class variable
Convergence
Inner class
Linear recursion
Nested class
46
Terminology
Key terms used in this lesson included:
Non-linear recursion
Recursion
R
i
Recursive case
Static modifier
Static method
Static nested class
Static variable
47
Summary
In this lesson, you should have learned how to:
Create static variables
Use
U static
t ti variables
i bl
Create static methods
Use static methods
Create static classes
Use static classes
48
Summary
In this lesson, you should have learned how to:
Create linear recursive methods
Create
C t non-linear
li
recursive
i methods
th d
Compare the pros and cons of recursion
49
Practice
The exercises for this lesson cover the following topics:
Implementing and accessing static variables
Implementing
I l
ti and
d using
i static
t ti methods
th d
Implementing and using static nested classes
50