Skip to content

Instantly share code, notes, and snippets.

@rishi93
Last active October 28, 2015 10:37
Show Gist options
  • Save rishi93/2a16ed36ba2fe16f9ec0 to your computer and use it in GitHub Desktop.
Save rishi93/2a16ed36ba2fe16f9ec0 to your computer and use it in GitHub Desktop.
Use of static variables
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