-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotes1008.txt
More file actions
35 lines (25 loc) · 973 Bytes
/
Notes1008.txt
File metadata and controls
35 lines (25 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Aim: What's up with that static thing anyway?
Do Now:
1) Open your Rational Number class (any
version is fine)
2) Add a method called toString, it should
return a string representation of the object.
3) In main, create a Rational Number object
and print out the object variable directly.
toString
Returns a String representation of an object.
Java will automatically call toString whenever
you use an object variable in a String context.
Every class has a toString method (just like a
default constructor, and equals). The default
toString method will return the class name and
the memory location of the object.
toString should have the following header:
public String toString()
If you write your own toString, it will
replace the one java creates for you.
RationalNumber r = new RationalNumber(8, 34);
System.out.println(r); // will call r.toString();
static
Static methods are class methods that are not
attached to a specific object.