3 cps209 Classes Objects 2
3 cps209 Classes Objects 2
Computer Science II
Prof. Alex Ufkes
Arrays.equals()
import java.util.Arrays;
Look familiar?
• Like Math, methods in the
Arrays class are all static.
• String methods belong to
String objects
• Arrays methods belong to
the Arrays class
String length()
What is arr.length? It’s clearly public, some sort of integer would make sense.
It is an int:
• It is declared final, which means it cannot
be mutated once assigned.
• It also means the maximum size for an array
in Java is 2,147,483,647 elements!
• 2,147,483,647 ints @ 4 bytes per int =>8Gb
Requires an import
Declare an
integer Using the Scanner we declared….
Works the same, but we’re reading from a string instead of the keyboard:
ArrayList<Type> variableName
Methods
We’ve seen how to use add() to insert items at the end of the list.
add() can also be used to insert items in the middle of the list.
© Alex Ufkes, 2020, 2025 52
Insert Elements
Auto-boxing
Auto-boxing
Same idea:
• Constructor is private!
• It can still be invoked by factory
method create()
• For this to work, create() must
be static. Why?
• If it’s not, we need to create an
object to invoke it.
• If constructor is private, we can’t
create a Card object.
Nested Classes:
Accessing Fields?
© Alex Ufkes, 2020, 2025 88
© Alex Ufkes, 2020, 2025 89
Private? Point can access
private members of PointList
• We’ve seen that local variables (in a method) can shadow class
instance variables.
Effectively final
• The variable is not explicitly
declared final, but never
actually changes.
• We can access it in our nested
class, but we can’t modify it.
Effectively final
• The variable is not explicitly
declared final, but never
actually changes.
• We can access it in our nested
class, but we can’t modify it.
• The encompassing method
cannot modify it either.
• Test: Could the variable be
declared final without compile
errors?
• If so, it is effectively final
© Alex Ufkes, 2020, 2025 102
Zero-One-Infinity
Inheritance
&
Polymorphism