@DavidMcConnachi/

CSE110_Java_Static_Members_Example

Java

No description

fork
loading
Files
  • Main.java
Main.java
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
class Rectangle {
    // these are instance variables
    int height = 1;
    int width = 1;

    // this is a class variable
    static int count = 0;

    // this is a parameterized constructor
    Rectangle(int initialHeight, int initialWidth) {
        count += 1;
	    	
        height = initialHeight;
        width = initialWidth;
    }
    // this is an instance method
    int getArea() {
        return height * width;
    }

    // this is a class method
    static void foo() {
        System.out.println("You called the Foo method.");
    }
}

public class Main {
	
	public static void main(String[] args) {
		Rectangle a = new Rectangle(3, 5);
		System.out.println(Rectangle.count + " Rectangle(s).");
		
		Rectangle b = new Rectangle(9, 2);
		System.out.println(Rectangle.count + " Rectangle(s).");
		
		Rectangle c = new Rectangle(1, 4);
		System.out.println(Rectangle.count + " Rectangle(s).");
    System.out.println(c.getArea());
	}	
}