forked from caveofprogramming/java-beginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
45 lines (33 loc) · 995 Bytes
/
App.java
File metadata and controls
45 lines (33 loc) · 995 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
36
37
38
39
40
41
42
43
44
45
class Person {
// Instance variables (data or "state")
String name;
int age;
// Classes can contain
// 1. Data
// 2. Subroutines (methods)
void speak() {
for(int i=0; i<3; i++) {
System.out.println("My name is: " + name + " and I am " + age + " years old ");
}
}
void sayHello() {
System.out.println("Hello there!");
}
}
public class App {
public static void main(String[] args) {
// Create a Person object using the Person class
Person person1 = new Person();
person1.name = "Joe Bloggs";
person1.age = 37;
person1.speak();
person1.sayHello();
// Create a second Person object
Person person2 = new Person();
person2.name = "Sarah Smith";
person2.age = 20;
person2.speak();
person1.sayHello();
System.out.println(person1.name);
}
}