How can I access the static members of a class

Static members have no relation with the object of the class. You cannot access the static members of the class by using the object. The only way to access the static members of the class is to use the class name. Look at the code below

class Test  {      public int X;      public static int Y;            public void Fun1()      {      }            public static void Fun2()      {      }  }

In the above class we have two static members and two non static members. Now if you want to read or write the value of Y then you do not need to create the object of this Test class, you can do so just by using the name of the class.

Test.Y = 100;

Now you might ask why we use the name of the class to access the static members of the class. The answer to this is that a data member by the name Y might exist in many other classes. We use the name of the class to specify that we are interested in the Y data member of the Test class and not any other class.

Similarly to call the Fun2 from the Test we will write

Test.Fun2();

Because Fun2 is static we use the class name to call this function.

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp