I want to print “hello” even before main is executed. How will you acheive that?



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

Post New Answer

More Core Java Interview Questions

What classes of exceptions may be thrown by a throw statement?

1 Answers  


Why main method is static in java?

1 Answers  


How variables are declared?

1 Answers  


What happens if main method is not static?

1 Answers  


whays is mean by inner class?

4 Answers   HCL, IBM, TCS,


What is the meaning of variable in research?

1 Answers  


Can an anonymous class be declared as implementing an interface and extending a class in java programming?

1 Answers  


What is variable in java?

1 Answers  


Who developed java?

1 Answers  


What is data type in java?

1 Answers  


Differentiate storage classes on the basis of their scope?

1 Answers   Fidelity,


Can inner class have constructor?

1 Answers  


Categories