-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path33_StaticKeyword
More file actions
32 lines (28 loc) · 1.03 KB
/
33_StaticKeyword
File metadata and controls
32 lines (28 loc) · 1.03 KB
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
// static = modifier, A single copy of a variable/method is created and shared,
// The class "owns" the static member
<Main.java>
public class Main {
public static void main(String[] args){
Friend friend1 = new Friend("Spongebob");
Friend friend2 = new Friend("Patrick");
Friend friend3 = new Friend("Squidward");
Friend friend4 = new Friend("Sandy");
Friend.displayFriends();//use static method
//System.out.println(Friend.numberOfFriends); //No longer need as used static void displayFriend()
}
}
/* friend1 & friend2 & friend3 sharing same numberOfFriends variable bcoz class itself own the static member
* if remove "static" from "static int", not share same, numberOfFriends = 1 for each friend1, friend2, friend3
*/
<Friend.java>
public class Friend {
String name;
static int numberOfFriends;
Friend(String name){
this.name = name;
numberOfFriends++;
}
static void displayFriends(){
System.out.println("You have "+numberOfFriends+" friends");
}
}