I want to print “hello” even before main is executed. How will you acheive that?
Answer / Vijayendra Kumar Gautam
To print “hello” before the main method is executed, you can use a static block or a static initializer block in your class:
```java
public class HelloWorld {
static { // Static Initializer Block
System.out.println("hello");
}
public static void main(String[] args) {
System.out.println("main started");
}
}
```
The output will be: `hellonmain started`
| Is This Answer Correct ? | 0 Yes | 0 No |
What classes of exceptions may be thrown by a throw statement?
Why main method is static in java?
How variables are declared?
What happens if main method is not static?
whays is mean by inner class?
What is the meaning of variable in research?
Can an anonymous class be declared as implementing an interface and extending a class in java programming?
What is variable in java?
Who developed java?
What is data type in java?
Differentiate storage classes on the basis of their scope?
Can inner class have constructor?