Skip to main content

Static Keyword in Java

Hello friends,
Welcome back 🙂

In this blog, I'm going to cover shortly about static keyword in Java. I'm assuming that you're familiar with the static keyword in java. Because I've written this blog in such a way that here only revision of the use of static keyword and the important points about static keyword which are rarely disclosed. So the first part will just be short description of static keyword, and second part will be the points about which, most of the new-bee or intermediate developers not familiar with. Please comment if anything is missing or not covered in this blog.

1. static member :
    All the non-static variables/member objects of a class are scoped at object level i.e. we can access a class's variable by using instance of that class. But in Java, we can create a class level scoped variable by using static keyword. The static variables can be accessed by using class name i.e. ClassName.staticVar. To declare variable as static, we just need to prepend static keyword before type of that variable.

2. static method :
    Same as static member,  we can declare a method as static which would scoped at class level. The methods declared using static keyword can be accessed by class name i.e. ClassName.staticMethod(). To declare a method as static, we just need to prepend static keyword before return type of that method.

3. static block :
     A static block is the collection of statements which gets executed when class is loaded into the memory by java class loader. This block executes only once when class is loaded. We can have multiple static blocks, there is no any restriction.

4. Inner static class :
     We can use static keyword for the class which is nested into another class. For packaging convenience, we use static class inside a class.

5.  static import :
    We can import static members (all or few) of a class  by using static import to avoid class reference.

6. static method in Interface:
    In Java 8, Java have introduced static methods in interface. Prior to Java 8, we could not write static methods. These static methods are same as static methods in class except:
    A. scope modifiers (private/public/protected) not allowed.
    B. static block still not added in interface.

Notes :
1. Any static member and static block of a class executes in the order they are declared (direction from top to bottom).
2. As the this and super keywords are instance level keywords, these keywords cannot used neither in static block nor in static method.
3. Sometimes you may get asked that can we override static method or not, and answer is no, because static method of base class can be only accessed and used by base class only, and same about derived class. The derived class's static method can only be accessed and used by derived class only. The signature of both base class's method and derived class's method may be same, but there would be no any relation between both that methods.
4. If a class having multiple static blocks, then execution direction will be top to bottom i.e. first static block will execute first, second block will execute after first and so on.

Hoping everything is covered in this article.
Happy learning.
Thanks.
Girish

Comments

Popular posts from this blog

Import multiple db files from folder into mysql db

Hello friends, Welcome back In the previous article, we've seen how to export database by table files, not the entire db file. Now in this article, here we're going to see the sample code for importing multiple sql files into same db. This also has been achieved by using a simple shell script. In this script, applied a for loop on list of files in a directory and by using that file name, mysql's import command is executed. The code is simple, you'll learn in just few minutes. Linux shell script: Execute above code in linux terminal to import all files into the given db. Hope this would help you. Thanks.

The final keyword in java

Hello friends, Welcome back 🙂. In this article, I'm going to give a short introduction to final keyword which would be helpful to know more about it. The final keyword in java has multiple uses. The main purpose of the final keyword is to add some restrictions on java elements (class, member and method). The restrictions have been described as below. Use of final keyword: 1. The final class : We can say the class is final when it declared as final. The restriction on final class is that the final class cannot be extended by any class. Rest of the behavior of the class remains same as any normal class. 2. The final method : The method declared using final keyword is to be said as final method. The restriction is that the final method cannot be overridden by derived class. This restriction applies to static method also. 3. The final member : The final member is the java class's member which is declared using final keyword. The restriction is that it can be initial...

XML tools on linux

Hello friends, Welcome back 🙂 In this article, we're going through XML tools which are useful for manipulation of XML. Recently I've worked on 3 tools so for now, adding those 3 tool's information, but as I get more, I'll update the article. These tools useful for validation and split/merge the XML. These tools available on both RHEL family and Debian family  linux which are same in use for both debian and RHEL. The tools are: 1. xmllint: This tool is used for xml validation. For some reason, we need to validate XML tags with its proper openings and closing i.e. syntactic validation, also we need to validate the data in the XML. The data can be different by its type which is defined in the XML's DTD, to detect this also, xmllint is useful. With this tool, there are multiple options we can use i.e. only syntactic validation, schema validation, DTD validation etc. When I was facing issue for validating the XML which having schema, I found this tool. H...