Java Java Objects Harnessing the Power of Objects Constants

Mitchell Smit
Mitchell Smit
9,219 Points

Static vs. Non-Static. Can someone give a clear difference between static and non- static, maybe a real life example?

Why is this useful?

1 Answer

Steve Hunter
Steve Hunter
Treehouse Moderator 57,657 Points

Hi Mitchell,

A static variable is a class-wide variable so every instance of the class holds the same value in its static variable.

You could use it in a constructor to count how many instances exist, for example:

public class Fiction{
  public static int noOfInstances;

  public Fiction(){
    this.noOfInstances += 1;


Every instance of Fiction will have a member variable called noOfInstances which will contain the same value at all times. That value will equal the number of times the constructor has been run.

I hope that helps.