STATIC KEYWORD IN JAVA

While getting started with any programming language, there are some keywords which we might just overlook or ignore at once and keep on going with the rest of it. In this blog I will be discussing one of the most overlooked and important keyword in Java. I must tell you this blog is not only for the people who are familiar with Java but for every programmer who faces the difficulty with the concept of ‘static’ keyword.

I too faced the problem in understanding the role of static keyword in Java when I started with the language initially but as I started understanding the role of static, understanding the code was very easy. In this blog rather than covering the syntax part I will be focusing on the application perspective usage. 

Let’s discuss ‘static’ keyword in brief.

What is ‘static’ in programming?

Static in programming concept is related to memory of computer. It basically means anything which is declared static will share the same memory.Also class member declared static can be accessed without creating an object as the data is same for all the objects, so it can be accessed with the ‘className’.

Let’s understand with the help of basic example :

class Abc { 
 int a;
 int b;
 static int c;
} 
public static void main(String args[]) { 
 Abc x = new Abc();
 Abc y = new Abc();
 Abc z = new Abc();
}

In java following can be static :

Data members(variables)

Data functions(methods)

Inner classes

block

Static variables

It is referred to as class data rather than object data.

To access static data :-

ClassName.dataname or objectName.dataname

An example of static variable is shown above with diagram.

Static Methods

Methods that works only on static data is declared static.

To invoke the method :-

ClassName.methodName(<args>) 

Functions that do not work on any member data, they are also declared as static(for reusability) .

The basic example of static method is the main(). It is declared static because it needs to be called before any object creation.

The most important thing to keep in mind is that static data can only be accessed with the help of static methods and static methods cannot access non-static data or method.

For example :-   
class Abc {    
 public static void sum(int a, int b) {      
// sum() declared static as it does not use any class data
System.out.println(a + b);  } }
public static void main(String args[]) {  
sum(10, 20);  //creates compilation error
Abc.sum(10, 20);  //correct way to call static method(don't need an object) }

Static block

static blocks are compiled before main().

So if static data needs to be initialized, rather than initializing in constructor one can create a static block and initialize the static data. As static block runs before main(), the static data will be initialized ony once.

If initialized in constructor, everytime object will be made, it would assign the value in static data more than once. 

I hope this information on ‘static’ keyword is enough to understand the concept. So I must end this blog here. I would be making more blogs on these kinds of topics which everyone faces problem with. Let me know your thoughts in comment section.

5 thoughts on “STATIC KEYWORD IN JAVA

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s