Responsive Ads Here

Sunday, February 2, 2020

what is static keyword in java | static keyword in java

What is static keyword in java |  static keyword in java

Ordinarily, when you create a class you are describing how objects of that class look and how they will behave. You don't actually get an object until you create one using new, and at that point storage is allocated and methods become available.

There are two situations in which this approach is not sufficient.

One is if you want to have only a single piece of storage for a particular field, regardless of how many objects of that class are created, or even if no objects are created. 

The other is if you need a method that is not associated with any particular object of this class. That is, you need a method that you can call even if no objects are created.


You can achieve both of these effects with the static keyword. When you say something is static, it means that particular field or method is not tied to any particular object / instance of that class. So even if you have never created an object of that class you can still call a static method or access a static field. 

Generally for non-static fields and methods, you must create an object and use that object to access the field or method, since non-static fields and methods must know the particular object they are working with.

Since static methods don't need any object to be created before they are used, they can not directly access non-static members or methods by simply calling those other members without referencing to a object,  non-static members and methods must be tied to a particular object.

Some object-oriented languages use the terms class data and class methods, meaning that the data and method exist only for the class as a whole, and not for any particular objects of the class. Sometimes the Java literature uses these terms too.


static keyword in java



To make a field or method static, you simply place the keyword before the definition. 

For example, the following produces a static field and initialises it.

class StaticTest{
static int i = 10;
}

Now even if you make two StaticTest object, there will still be only one piece of storage for StaticTest.i  both objects will share the same i.
Example 

 StaticTest obj1 = new StaticTest();
 StaticTest obj2 = new StaticTest();

At this point, both obj1.i and obj2.i have the same value of 10 since they refer to the same piece of memory.

There are two ways to refer to a static variable. As the preceding example indicates, you can name it via an object, by saying for example obj1.i . You can also refer to it directly through its class name, something you cannot do with a non-static member.
 StaticTest.i ++.

The ++ operator adds one to the variable. At this point, both obj1.i and obj2.i will have the same value 11.

Using the class name is the preferred way to refer to a static variable. Not only does it emphasise that variable's static nature, but in some cases it gives the compiler better opportunities for optimisation.

Similar logic applies to static methods. You can refer to a static method either through an object as you can with any method, or with the special additional syntax ClassName.method(). You define a static method in a similar way:

class Incrementable {
static void increment() {
StaticTest.i ++;
}
}


You can see that the Incrementable method increment() increments the static data i using the ++ operator. You can call increment() in the typical way, through an object.

Incrementable incr = new Incrementable();
incr.increment();


Or, because increment() is a static method, you can call it directly through its class:

Incrementable.increment();

Although static, when applied to a field, definitely changes the way the data is created ( one for each class versus the non-static one for each object ) when applied to a method it's not so dramatic. 

An important use of static for methods is to allow you to call that method without creating an object. 

This is essential, as you will see, in defining the main() method that is the entry point for running an application.



Stay tuned for for about static keyword uses and impact.




No comments:

Post a Comment