- A block which starts with static keyword is called as static block.
- Static block execute automatically when class is loaded in memory.
- We can directly execute a statement in static block in versions of the JDK 1.6 and previous versions.
- So we can print a string without using main method in JDK 1.6 and previous versions.
- 1.7 JDK onwards we must have a main method in class.
- Static block is executed before main method.
- If we stop JVM in static block main will not be executed.
- Till JDK 1.6 all the Static members are executed first in sequence and then main method is executed.
- After JDK 1.6 interpreter identifies static members from top till bottom and then checks for main method, if main method is there it will execute static members in order and then execute main method.
- Weekend have more than one static blocks in a class, they will be executed top to bottom in order of their occurrence.
- Static block is executed at class loading, hence at the time of class loading if we want to perform any activity, we have to define that inside static block.
- For example native methods are always created inside static blocks.
- Native methods are methods who’s code is implemented in another language.
- Static block is used to initialize the static members.
- Code Example : javacodes/PojoClasses/src/javaimplant/staticblock at master · gauravmatta/javacodes (github.com)
- Introduction
- Installation
- Junit
- Arrays
- Classes
- Data Types
- Expressions
- Interfaces
- JDBC
- Loops
- OOPS
- Serializable
- Strings
- Constructors
- Package
- Java Servlets
- Deployment
- Logging
- JSP
- ANT
- Web Services and Sockets
- Struts
- JPA
- Object Injection
- Annotations
- Reflection API
- Static Blocks
- Java Native Interface (JNI)
- Multithreading
- Tomcat Server
- Java Web Toolkit(JWT)
- Archive Files
- EJB(Enterprise Java Beans)
- JConsole
- Memory Profiling
- Hashing
- Exceptions
- Java Map Interface
- Java 8
- Garbage Collection
- Java 11
- Collections
Static Blocks
Subscribe to:
Posts (Atom)
-
What is the way to import only static members of a class? We can import using “import static packagename.classname.*” We can import using “i...
-
Q What is the difference between Array List and Linked List? Array List and Linked List both extend the List interface which extends Coll...
-
How do you detect a deadlock in a running Java program? We can use Jconsole to check for Deadlock which comes with JDK. There are also other...
No comments:
Post a Comment