static keyword in Java is used a lot in java programming. Java static keyword is used to create a Class level variable in java. static variables and methods are part of the class, not the instances of the class. Show
static keyword in javaJava static keyword can be used in five cases as shown in below image. We will discuss four of them here, the fifth one was introduced in Java 8 and that has been discussed at Java 8 interface changes.
Let’s see all the static keyword in java usage in a sample program. StaticExample.java package com.journaldev.misc; public class StaticExample { //static block static{ //can be used to initialize resources when class is loaded System.out.println("StaticExample static block"); //can access only static variables and methods str="Test"; setCount(2); } //multiple static blocks in same class static{ System.out.println("StaticExample static block2"); } //static variable example private static int count; //kept private to control its value through setter public static String str; public int getCount() { return count; } //static method example public static void setCount(int count) { if(count > 0) StaticExample.count = count; } //static util method public static int addInts(int i, int...js){ int sum=i; for(int x : js) sum+=x; return sum; } //static class example - used for packaging convenience only public static class MyStaticClass{ public int count; } }Let’s see how to use static variable, method and static class in a test program. TestStatic.java package com.journaldev.misc; public class TestStatic { public static void main(String[] args) { StaticExample.setCount(5); //non-private static variables can be accessed with class name StaticExample.str = "abc"; StaticExample se = new StaticExample(); System.out.println(se.getCount()); //class and instance static variables are same System.out.println(StaticExample.str +" is same as "+se.str); System.out.println(StaticExample.str == se.str); //static nested classes are like normal top-level classes StaticExample.MyStaticClass myStaticClass = new StaticExample.MyStaticClass(); myStaticClass.count=10; StaticExample.MyStaticClass myStaticClass1 = new StaticExample.MyStaticClass(); myStaticClass1.count=20; System.out.println(myStaticClass.count); System.out.println(myStaticClass1.count); } }The output of the above static keyword in java example program is: StaticExample static block StaticExample static block2 5 abc is same as abc true 10 20Notice that static block code is executed first and only once as soon as class is loaded into memory. Other outputs are self-explanatory. Java static importNormally we access static members using Class reference, from Java 1.5 we can use java static import to avoid class reference. Below is a simple example of Java static import. package com.journaldev.test; public class A { public static int MAX = 1000; public static void foo(){ System.out.println("foo static method"); } } package com.journaldev.test; import static com.journaldev.test.A.MAX; import static com.journaldev.test.A.foo; public class B { public static void main(String args[]){ System.out.println(MAX); //normally A.MAX foo(); // normally A.foo() } }Notice the import statements, for static import we have to use import static followed by the fully classified static member of a class. For importing all the static members of a class, we can use * as in import static com.journaldev.test.A.*;. We should use it only when we are using the static variable of a class multiple times, it’s not good for readability. Update: I have recently created a video to explain static keyword in java, you should watch it below. https://www.youtube.com/watch?v=2e-l1vb\_fwM Want to learn more? Join the DigitalOcean Community!Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest. Sign up Which of the following is true about static methods in Java?Static methods are always public, because they are defined at class-level.
How the static methods can be accessed in Java?A static method in Java is a method that is part of a class rather than an instance of that class. Every instance of a class has access to the method. Static methods have access to class variables (static variables) without using the class's object (instance). Only static data may be accessed by a static method.
What is true about a static method?A static method can call only other static methods. A static method can be called directly from the class, without having to create an instance of the class. can receive parameters and can return a value. All the above are true.
How are static methods accessed?Static methods can be accessed without having to create a new object. A static method can only use and call other static methods or static data members. It is usually used to operate on input arguments (which can always accept), perform calculation and return value.
|