What is static in java
Change Language. Related Articles. Basics of Java. Operators in Java. Packages in Java. Flow Control in Java. Loops in Java. Jump Statements in Java. Arrays in Java. Strings in Java. OOPS in Java. Constructors in Java. Interfaces in Java. Keywords in Java. Exception Handling in Java. Collection Framework. Multi-threading in Java. Table of Contents. Save Article. Improve Article. Similar to static variables, static methods can also be invoked using the class name.
There are some important points that you need to consider when you work with static methods in Java. These are -. Also, often you will notice that the main method in Java is defined as static. If you have defined the main method in Java as non-static, then the Java Virtual Machine JVM would have first created an instance for the main class and then called the main method using that instance which would lead to unnecessary memory usage.
Moreover, there are tons of static methods defined in the Wrapper Classes and Utility Classes in Java. The above program will generate an error.
This is so because it has tried to access a non-static variable called counter inside a static method called increment. Less memory consumption since they are allocated memory only once when the class is being loaded. Generally, static blocks in Java are used to initialize static variables. They are executed only once when the class is loaded and hence, are perfect for this job.
Also, you can include more than one static block in the class. Static blocks can only access static variables. Here, you saw the creation of two static variables called i and j inside the Test class. It went on to initialize variable j using a static block. In the main method, you must use the class name to print the values of i and j static variables. You can see that the static block gets executed before the execution of the main method.
When the static block is executed, it prints the first line regarding the initialization and then initializes the variable j. Then, the main method gets executed which prints the values of both the variables.
In Java, you can use static keywords for nested classes as well. Also, a nested static class cannot access the members of the outer class that are non-static. Here, you saw the creation of a simple static nested class and tried to access a static member of the top-level class. Here, you saw the creation of two static variables called i and j inside a test class.
You have then used two static blocks to initialize these variables. After that, you must use a static method to display the values of these variables. Post that, you have used a nested static class and defined a method inside it that updates the values of these static variables and displays them.
Before loading the main method, the static block gets executed and prints the message for initialization. Yes, add me to your mailing list. Your Name required. Your Email required. Your Message. Attach file. About WordPress.
View AMP version. Table of Contents Click on links below to navigate 1 What is static in Java? Static Inner class. Static method in Interface.
Please leave this field empty. Previous article. Leave a Reply Cancel reply. Query Form. Go to mobile version.
0コメント