Last active
October 28, 2015 10:37
-
-
Save rishi93/2a16ed36ba2fe16f9ec0 to your computer and use it in GitHub Desktop.
Use of static variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
class Animal{ | |
static int population = 0; | |
public Animal(){ | |
System.out.println("Animal object created"); | |
population += 1; | |
} | |
} | |
class Person{ | |
static int population = 0; | |
public Person(){ | |
System.out.println("Person object created"); | |
population += 1; | |
} | |
} | |
class Application | |
{ | |
public static void main(String args[]){ | |
Person p1,p2,p3; | |
Animal a1,a2; | |
p1 = new Person(); | |
p2 = new Person(); | |
p3 = new Person(); | |
a1 = new Animal(); | |
a2 = new Animal(); | |
System.out.println(Person.population); | |
System.out.println(Animal.population); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment