---
title: "Can we execute a program without main() method?"  
description: "Can we execute a program without main() method?"  
author: "Samuel Fernandes"  
published: 2015-03-27  
updated: 2020-09-16  
canonical: https://www.mindstick.com/interview/2365/can-we-execute-a-program-without-main-method  
category: "javascript"  
tags: ["java"]  
reading_time: 1 minute  

---

# Can we execute a program without main() method?

Yes, one of the way is static block but in previous version of JDK not in JDK 1.7\

```
class A3{    static{    System.out.println("static block is invoked");    System.exit(0);    }  }  
```

\

```
Output:static block is invoked (if not JDK7)
```

\
In JDK7 and above, output will be:\

```
Output:Error: Main method not found in class A3, please define the main method as:public static void main(String[] args)
```

## Answers

### Answer by Anonymous User

Yes, one of the way is static block but in previous version of JDK not in JDK 1.7\

```
class A3{    static{    System.out.println("static block is invoked");    System.exit(0);    }  }  
```

\

```
Output:static block is invoked (if not JDK7)
```

\
In JDK7 and above, output will be:\

```
Output:Error: Main method not found in class A3, please define the main method as:public static void main(String[] args)
```


---

Original Source: https://www.mindstick.com/interview/2365/can-we-execute-a-program-without-main-method

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
