1 Answer - Sort by: Date | Rating
Yes, u can ,Normally, you need a main method in a Java class if you want to run that class from the command line. However, there is a little trick that allows you to run one without a main
class NoMainMethod
{
static
{
System.out.printl ma! No main method");
System.exit(0);
}
}
The reason this works is that static initialization blocks get executed as soon as the class is loaded—even before the main method is called. As soon as the block exits, it will look for the main method. When it doesn't find it, it throws an exception—so the statement exits the program before the exception is thrown
class NoMainMethod
{
static
{
System.out.printl ma! No main method");
System.exit(0);
}
}
The reason this works is that static initialization blocks get executed as soon as the class is loaded—even before the main method is called. As soon as the block exits, it will look for the main method. When it doesn't find it, it throws an exception—so the statement exits the program before the exception is thrown
0
0
Guest
answered 6 months ago
- How To Create An Application By Using Java Program?
- How To Make An Application By Using Java Program?
- What How The Step For Make Library Management?
- In Which Process We Can Start Java?
- How To Download Java IDE?
- Define The Concept Of Java Byte Code?
- How To Run Remote Method Invocation In More Machine?
- How To Run Java Remote Method Invocation In More Machine?
- How To Make A Java Program That Will Ask The User To Input A Number And Display All The Even Number And The Sum Of All Even Numbers Based On The Inputted/ Given Numbers?
- Is A Static Variable An Instance?
- How To Compile And Run Java Package?
- What Does Literati-on Mean?
- What Is Abstract Class In Details?
- What Is Meant By Remote Method Invocation In Java?
- What Is Meant By Package In Java?
- What Is Meant By Servlet In Java?
- When Was Java Released?
- What Is Latest Technologies In Java?
- Write A Program Using Do…..while Loop To Calculate And Print The First M Fibonacci Numbers?
- What Is The Difference Between Reference &instance Variable?
- Why Java Is Slow?
- What Is The Drawback Of Ms Access?
- What Is Replaced In Place Of Pointers In Java?
- What Replaces Pointers In Java?
- How Many Types We Create Object In Java?
- How Can I Change The Main Method Name In My Java Program?
- How Do I Write A Java Program Without The Main Method?
- Does Every Java Program Include A Java Main Method?
- Can A Java Program Can Run Without Main Program? If Not Solve Me This Problem?
- What Is A Simple Java Program Of Array With Method?
- Why Cant I Create An Object For Main Method In Java?
- Can We Change The Syntax For Main Method In Java\?
- Why Should We Give String Arguments In Java Main Method
- How We Can Run The Program Of C++ In Java....?
- How To Compile And Run The Java Program?

New Comment - Comments are editable for 5 min.