Static & Non-Static keywords.
Let's say we have one class named Cat.
The Cat class is having two non-static attributes & one non-static method.
So to access or call the non-static methods & attributes we have to create the instance of class Cat.
Now I'm creating a private static attribute catCount for counting the total nos. of cat
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.
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.
- Now lets create one more object of cat class and see the value of catCount.
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