Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the responsive-lightbox domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the hueman domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /var/www/wp-includes/functions.php on line 6114
Static keyword - Testingpool

Static keyword

static  keyword s mainly used for memory management. It can be used with variables, method,blocks and  nested class. The static keyword belongs to the class, not to the object of the class.

It can be used with the following:

  1. Variable(also known as class variable)
  2. method (also known as class method)
  3. block
  4. nested class

1. Java static variables:

if you use static keyword with a variable ,that variable is known as static variable.

  • For static variables , memory is assigned only once at the time of class loading.
  • Can be used to refer the common property of all the objects e.g. college for all the students, a garden for walk,library name for all the books etc.

Problem if static not used:

class bookStore{  
     int bookId;  
     String bookName;  
     String libName="XYZ";  
}

Suppose , there is a book store which has library named as XYZ. There are total 1000 books, in that library each book has unique id and name. So, when the object is created , all the data members occupy memory. As we can see library is common for all the books, we should make it static so it consumes memory only once.

Example of usage of static keyword:

package com.testingpool.demo2;

public class BookStore {
	 int bookId;
	 String bookName;
	 static String libName = "XYZ";
	 
	 BookStore(int id,String bName){
			bookId=id;
			bookName=bName;
			System.out.println(bookId + "--"+bookName+"--"+libName);
		}
	 
	 public static void main(String[] args){
		BookStore store1 = new BookStore(1, "Maths book");
		BookStore store2 = new BookStore(2, "English book");
	 }
}
Output: 1–Maths book–XYZ 2–English book–XYZ

2. static methods: If a static keyword is used with method, known as static method.It has the following points to note.

  • A static method can access static data-members and can change its value.
  • Static method belongs to the class, not with the object.
  • Static method can be invoked without using the object of the class.
package com.testingpool.demo2;

public class BookStore {
	 int bookId;
	 String bookName;
	 static String libName = "XYZ";
	 
	 static void changeLib(){  // change the static data member
		 libName = "ABC";
	 }
	 
	 BookStore(int id,String bName){
			bookId=id;
			bookName=bName;
			System.out.println(bookId + "--"+bookName+"--"+libName);
		}
	 
	 public static void main(String[] args){
		changeLib();   // calling static method directly without creating the object
		BookStore store1 = new BookStore(1, "Maths book");
		BookStore store2 = new BookStore(2, "English book");
	 }

}
Output: 1–Maths book–ABC 2–English book–ABC

3. Static block: There are 2 important aspect to understand about the static block.

  1. static blocks get executed before the main method at the time of classloading.
  2. static block can be used to initialize the static data members.
package com.testingpool.demo2;

public class BookStore {
	 int bookId;
	 String bookName;
	 static String libName = "XYZ";
	 
	 static{
		 System.out.println("First execute the static block");
	 }
	 
	 BookStore(int id,String bName){
			bookId=id;
			bookName=bName;
			System.out.println(bookId + "--"+bookName+"--"+libName);
		}
	 
	 public static void main(String[] args){
		BookStore store1 = new BookStore(1, "Maths book");
		BookStore store2 = new BookStore(2, "English book");
	 }

}
Output: First execute the static block 1–Maths book–XYZ 2–English book–XYZ

Important points to notice:

  • static method can only access static data-members or methods, not the non-static.
  • this and super cannot be used in static context.
package com.testingpool.demo2;

public class SubNumber {
	
String name="Shekhar";

	public static void main(String[] args) {				
		System.out.println( name );
	}

}
Error : Cannot make a static reference to the non-static field name

Can we execute program without main method?

It was possible in JDK version before 1.7. We could have used the static block and execute the program. But in JDK 1.7 it throws an compilation error.

Ask Question
If you have any question, you can go to menu ‘Features -> Q&A forum-> Ask Question’.Select the desired category and post your question.
Avatar photo

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. This website is his window to the world. He believes that ,"Knowledge increases by sharing but not by saving".

You may also like...