You are on page 1of 3

Practical-2

1. What is the difference between instance method and static method?

Instance Method Static Method


These methods need an object to invoke These methods do not require objects to be
and each object has a separate copy of created and all objects refer to the same
the function. copy of the function.
They are declared as normal functions. These use the keyword ‘static’ to declare a
function as static.
They are invoked using a class object. They are called by the class.
We can use ‘super’ and ‘this’ keywords in We cannot use ‘super’ and ‘this’ keywords
instance functions. within static methods.
A instance method is accessed only by A static method can be accessed without
creating an instance of the class. creating an instance of the class.
These can access static as well as non- These can only access static data
static data. members.
Instance methods are declared as follows: Static methods are declared as follows:

return_type func_name(arg list); Static void func_name(arg_list);


public void Method2() public static void Method1()
{ {
// this is an instance method // this is a static method
} }

2. What is the use of static method?

There will be times when you will want to define a class member that will be used independently
of any object of that class. Normally a class member must be accessed only in conjunction with
an object of its class. However, it is possible to create a member that can be used by itself,
without reference to a specific instance.

To create such a member, precede its declaration with the keyword static. When a member is
declared static, it can be accessed before any objects of its class are created, and without
reference to any object.

The most common example of a static member is main( ). main( ) is declared as static because
it must be called before any objects exist. Methods declared as static have several restrictions:
• They can only call other static methods.
• They must only access static data.
• They cannot refer to this or super in any way.
There are good reasons to define a method as static
● Documentation.

Anyone seeing that a method is static will know how to call it . Similarly, any programmer
looking at the code will know that a static method can't interact with instance variables,
which makes reading and debugging easier.

● Efficiency.

A compiler will usually produce slightly more efficient code because no implicit object
parameter has to be passed to the method.

3. Calling static methods

Example:

class MyUtils {
. . .
//================================================= mean
public static double mean(int[] p) {
int sum = 0; // sum of all the elements
for (int i=0; i<p.length; i++) {
sum += p[i];
}
return ((double)sum) / p.length;
}//endmethod mean
. . .
}

Inside the class

From inside the class we can call a static method by just writing the static method name. Eg,

// Called from inside the MyUtils class


double avgAtt = mean(attendance);

Outside the class

Outside of the class in which they are defined, static methods can be used independently of any
object. To do so, you need only specify the name of their class followed by the dot operator. For
example, if you wish to call a static method from outside its class, you can do so using the
following general form:

classname.method( )
Here, classname is the name of the class in which the static method is declared. As you can
see, this format is similar to that used to call non-static methods through object- reference
variables. This is how Java implements a controlled version of global.

Here is an example.

// Called from outside the MyUtils class

double avgAtt = MyUtils.mean(attendance);

If an object is specified before it, the object value will be ignored and the the class of the object
will be used.

You might also like