Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
60 lines (48 loc) · 1.69 KB

File metadata and controls

60 lines (48 loc) · 1.69 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// demonstrates static/non-static fields and methods
// simulates a person (not in the Blade Runner sense)
public class Person {
// instance variable: age of this person
private int age;
// another instance variable: name of this person
private String name;
// static variable (shared by all instances): global population
private static int population = 0;
// constructor
public Person(int a, String n) {
// copy arguments of constructor to instance variables
age = a;
name = n;
// increase the static counter
population++;
}
// static method (not per-instance)
public static void printPop() {
System.out.println(population);
}
// instance method
public void printName() {
System.out.println(name);
}
// another instance method
public void printInfo() {
System.out.println(age);
// calling an instance method without a period
// (uses same instance as what printInfo was called on)
printName();
}
public static void main(String[] args) {
// calling a static method using class name and period
// what is the output?
Person.printPop();
// how many instances does this construct?
Person myDad = new Person(33, "Lucius");
Person myMom = new Person(44, "Pandora");
Person myDentist = myMom;
// calling an instance method using instance name and period
myDentist.printInfo();
// calling a static method without a period
// (uses Person, the containing class, by default)
// what is the output?
printPop();
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.