Static & Non-Static keywords.

  1. Let's say we have one class named Cat.

  2. The Cat class is having two non-static attributes & one non-static method.

  3. So to access or call the non-static methods & attributes we have to create the instance of class Cat.

  1. Now I'm creating a private static attribute catCount for counting the total nos. of cat

  2. And creating a constructor of Cat class and increasing the catCount by 1. So that every time object creation will happen it will give me the catCount.

  3. Now creating the getCount method to get the count of the total cats. // implementation of encapsulation.

Note:

Here you can see in the line no. //40 , as meow method is non-static, So we can't access the non-static method with the type of the class.

But as my getcatCount and catCount method and attribute are static fields , So we can access it with the class type without the object reference.

  1. Now lets create one more object of cat class and see the value of catCount.

0
Subscribe to my newsletter

Read articles from Pragya kumari Swarnakar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Pragya kumari Swarnakar
Pragya kumari Swarnakar