-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructors2.java
More file actions
35 lines (28 loc) · 894 Bytes
/
Copy pathConstructors2.java
File metadata and controls
35 lines (28 loc) · 894 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
public class Dog
{
public String breed, name = "";
// name is initialized explicitly above to an empty string.
// Below, a constructor method initializes breed the same way.
// These two ways to initialize variables are equivalent.
public Dog()
{
breed = "";
}
// This constructor allows us to set the breed to whatever we
// want when we create a Dog instance with the new keyword.
public Dog(String breed)
{
this.breed = breed;
}
// This constructor allows us to set both breed and name on
// the new keyword.
public Dog(String breed, String dogName)
{
this.breed = breed;
name = dogName;
}
public void bark()
{
System.out.println("My " + breed + " " + name + " is barking!");
}
}