Course Content‎ > ‎Session 4‎ > ‎

Class Variables and Methods

Each class has variables and methods. There are two kinds of variables and methods in Java:
 
    1. Instance Variables and Methods
    2. Static or Class Variables and Methods
 
A class basically contains two sections, one that declares variables and the other that declares methods. These variables and methods are called as instance variables and instance methods.  Every time a class is instantiated, a new copy of each of them is created. They are accessed by using dot operators. 
 
But if we have to define a member that is common to all the objects and accessed without using any particular object, i.e. we want to define a member that belongs to the class as a whole rather than the objects created from the class.  Such members can be declared as static and are also called as static members. For example,
 
    /* static member variable */
    static int count;      
     
    /* static member method */
    static int max(int x, int y); 

Static variables and static methods are also called class variables and class methods, respectively.  Static variables are used when we want to have a variable common to all instances of a class. For example, a variable that could keep a count of how many objects of a class have been created. Java creates only one copy for a static variable, which can be used even if the class is never instantiated. A static variable can be accessed using the dot notation just like instance variables.  But a static variable can be accessed with the instance of an object or with the class itself. Static methods are also called without using objects. They are available for use by other classes. For example,
 
    class FamilyMember
    {
           static String  Lastname = "Taylor";
           String Firstname;
           int age;
           .........
           .........
    }

    System.out.println("Family's Last Name is " + FamilyMember.Lastname);
 
In this example, each instance of the class FamilyMember will have their own values for Firstname and age. But the class variable Lastname has only one value for all the family members. If the Lastname is changed all the instances of the class FamilyMember will have changed Lastname.

When are methods declared as class methods?
 
Methods that are of general utility but do directly affect an instance of the class are declared as class methods. Java class libraries contain a large number of class methods. For example, the Math class of Java library defines many static methods to perform math operations that can be used in a program. For example,
 
    float a =  Math.sqrt(25.0);
 
The method sqrt is a class method defined in class Math.
 
    int largest = Math.max(x,y);  

The method max is a class method defined in class Math, it takes two arguments and returns the larger of the two. Another example of a static method is, the String class contains a class method called valueOf(), which can take one of many different types of arguments (integer, boolean, other objects etc.).  The valueOf() method then returns a new instance of String containing the string value of the argument it was given. This method does not operate directly on an existing instance of String. That is why this method is defined as a class method in the class String

Static methods have some restrictions:
  • They can only call other static methods.
  • They can only access static data of the class

Given below is an example, which shows how static methods can be defined and called in a program:


    class demo
    {
        /* mul is class method, which returns a decimal number, which is the
           product of the two decimal numbers passed to it.*/
         
        static float mul(float x, float y)
        {
            return x * y;
        }  
         
        /* divide is class method, which returns a decimal number, which is
           the result of division of  the two decimal numbers passed to it.*/
         
        static float divide(float x, float y)
        {
            return x / y;
        }
        
    } // end of definition of class demo
     
    class demoStatic
    {
        public static void main(String args[])
        {
            /* to call a class method, you use dot notation.  You can use
               either an instance of a class or the class name itself to 
               call a class method */  
             
            // a1, an object of class demo is declared and created here 
            demo a1 = new demo( );  
             
            // class method mul is called using the object a1 of class 
            float a = a1.mul(32, 4);   
             
            // class method divide is called using the class demo itself
            float b = demo.divide(4, 2);   
             
            System.out.println(" value of a is = "+ a);
            System.out.println("value of b is = " + b);
        }  
    }

Comments