Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?
Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..
CAn anyone tell me the reason?
And correct me If am wrong?
My point of view is that,
If we create an applet in java than it can run without having main method,but if we develop an application than it do requires main method.
Reason why we require main method is because we write public static void main() i.e main method is a static method & so we can call it without creating object of that class . so when we compile (i.e javac filename.java) java compiler needs to use this main method without creating any object of that class.
please correct me if i am wrong.
since java is pure object oriented language....and also main() method is an entry point...through which execution of program begins............
This is because main is always the entry gate for the program. but if we are able to provide any other initiation point there is no need of main
as in case of an applet in which there is no main method, it has init
So, main method is must to run a java program...!
i.e By using static
she gave this example program...!
public void static( )
public class
{
public void static( )
{
System.out.println( "Without main method" );
System.exit(0);
}
}
😕😕😕😕sahithi pallaviguys..! My friend told me that we can write and run a java program without using the main method.
i.e By using static
she gave this example program...!
public void static( )
public class
{
public void static( )
{
System.out.println( "Without main method" );
System.exit(0);
}
}
have you execute it...is it running successfully
And I told you that Java Applet programs don't generally have the main method.
they have paint() and init() method.
But where is the function name in this program.ms_csThe java program can run without main method with this static method
With which name you will save the file?
as static is the reserved keyword..how can you use it...
if this is possible.
then what would we say it,,,,it's feature or limitation??
Let's see an example to avoid any confusions
STEP 1: Create a class like followng in any of the locations of your sytem. Let me keep it "I:\"
/** * * @author sookie */ public class ProgramWithoutMainMethod { static { System.out.println("Look at me ! I am running without Main method"); System.exit(0); } }STEP 2: Now open command prompt and go to"bin" directory of your installed JDK and enter following command. It compiles the program
javac I:\ProgramWithoutMainMethod.java
![[IMG]](proxy.php?image=http%3A%2F%2Fimg26.imageshack.us%2Fimg26%2F1910%2Fpic18a.jpg&hash=1406280b06113d8c64d4d870ad03d16a)
STEP 3: Now let's simply run the program.
java -classpath I:\ ProgramWithoutMainMethod
![[IMG]](proxy.php?image=http%3A%2F%2Fimg690.imageshack.us%2Fimg690%2F3112%2Fpic19z.jpg&hash=8f8c64f49006bfa77030860c7b136f7c)
Now you all might be wondering that why do I need to add this additional line "System.exit(0); " right? See, as soon as my "static block" reaches its closing brace, it tries to search for main() method which as of now you all now is must for running a Java program so in order to stop my program from looking for main() method, I would simply call System.exit(0); and terminate the entire Java program. So I am not giving it a chance to look for any main() method.
Hoping now you all might be cleared about the things discussing over here.
PS: Before directly asking questions [specially related to Java] - A tip try to search web, it is having so much information that you will learn 100 other things with only 1 question. 😀
try it
but it can give a n error so use System.exit(0) at the end of the file
as
public Nomain{
static{
System.out.println("printing without main");
}
System.exit(0);
}
crazy1.java:1: 'class' or 'interface' expected
public void static( )
^
crazy1.java:2: <identifier> expected
public class
^
crazy1.java:9: '{' expected
}
^
3 errors
These are the errors in this program.
can you tell me is it working properly?
If yes tell me how.
Pardon my ignorance but I couldn't see where is your program crazy1.java ?rama_krish627107627@ccc108:~> javac crazy1.java
crazy1.java:1: 'class' or 'interface' expected
public void static( )
^
crazy1.java:2: <identifier> expected
public class
^
crazy1.java:9: '{' expected
}
^
3 errors
These are the errors in this program.
can you tell me is it working properly?
If yes tell me how.
I am just assuming
You are using "class" keyword in your program and name of the file and class marked as public are same.
For Example APPLET...😁
try this
public class A
{
static
{
System.out.println( "Without main method" );
System.exit(0);
}
}
so when we run a program JVM does not find any entry gate(i.e main() method) into the program.lets take example..suppose you have a house connecting 2 to three rooms but atleast u requires a entry gate. so it is also necessary to have a main() methos like main() (entry gate in to run your program)i.e from where JVM can start.
bt he doesnt know how...........
yes, we can. By using static block.rahulgakharbut my friend told me we can run without main..............
bt he doesnt know how...........
Go through the previous posts of this thread.
We can run the program without using the main function in java......
and we can print any value any string....
if we want to print anythings in java without using main....
then we use the static block.....
with the use of static block program compile successfully but occur a run time error...
for avoiding these run time error we can use the System.exit(0);
Example............
class staticEx { static { System.out.println("Inside Static Block"); System.exit(0); } }AMIT PORWAL, BANGALORE(soft. eng.)
Moderator Note:: We recommend all communications to happen on CE!!
Please donot post email ids.
{
static
{
System.out.println("This is the Java Program That Runs Without Main Method");
System.exit(0);
}
}
Try this one....it works
Yes, you can but do not count on it when you develop your application. In Java, a static initializer gets executed as soon as the class is loaded, even before the main method is called. Here is an example,sahithi pallaviMy friend asked me this question.!
Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?
Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..
CAn anyone tell me the reason?
And correct me If am wrong?
public class WithoutMain {
static {
System.out.println( "Hello World" );
System.exit(0);
}
}
The JVM finds and loads the class into memory when you run this code, the static initializer is executed during the loading and initialization of the class. System.exit(0) is called at the end of the static block to terminates the program. If not, then the JVM would have next used reflection on that class to find the main() method. If it does not find the main method, it throws an exception.
[TABLE]
[TR]
[TD]class MainMethodNot
{
static
{
System.out.println("This java program have run without the run method");
System.exit(0);
}
}[/TD]
[/TR]
[/TABLE]
Sadaguys..! My friend told me that we can write and run a java program without using the main method.
i.e By using static
she gave this example program...!
public void static( )
public class
{
public void static( )
{
System.out.println( "Without main method" );
System.exit(0);
}
}
will work, Vinci. Check once again.vinciin reality this program wont work .my compiler gives me an error saying that main method not defined
And see this explanation,
If I am not wrong ms_cs mean to say by using 'static block' not 'static method' because for calling a static method, one need to have an entry point while 'static block' is automatically called whenever class is loaded in JVM. "Static blocks" declared in any Java class are always called before the main() method. You can also note that why static variables or methods are declared outside "main" method.
Let's see an example to avoid any confusions
STEP 1: Create a class like followng in any of the locations of your sytem. Let me keep it "I:\"
Code:
/** * * @author sookie */ public class ProgramWithoutMainMethod { static { System.out.println("Look at me ! I am running without Main method"); System.exit(0); } }
STEP 2: Now open command prompt and go to"bin" directory of your installed JDK and enter following command. It compiles the program
javac I:\ProgramWithoutMainMethod.java
STEP 3: Now let's simply run the program.
java -classpath I:\ ProgramWithoutMainMethod
Now you all might be wondering that why do I need to add this additional line "System.exit(0); " right? See, as soon as my "static block" reaches its closing brace, it tries to search for main() method which as of now you all now is must for running a Java program so in order to stop my program from looking for main() method, I would simply call System.exit(0); and terminate the entire Java program. So I am not giving it a chance to look for any main() method.
Hoping now you all might be cleared about the things discussing over here.
PS: Before directly asking questions [specially related to Java] - A tip try to search web, it is having so much information that you will learn 100 other things with only 1 question. 😀
[TR]
[TD="class: votecell"][/TD]
[TD="class: answercell"][/TD]
[/TR]
[/TABLE]
Hi,
This is just convention. In fact, even the name main(), and the arguments passed in are purely convention.
When you run java.exe (or javaw.exe on Windows), what is really happening is a coupleo of Java Native Interface (JNI) calls. These calls load the DLL that is really the JVM (that's right - java.exe is NOT the JVM). JNI is the tool that we use when we have to bridge between the virtual machine world, and the world of C, C++, etc... The reverse is also true - it is not possible (at least to my knowledge) to actually get a JVM running without using JNI.
----------
Struts2 Jquery
Welcome to you good question asked by your friends i am finding answer for you..
---------
In order to run a JAVA program INDEPENDENTLY you need to have main() method. You can create java classes without main method but they can be used in another program but cannot be run independently.
Hope this helps.
I will tell u very conceptual answer.
1) Main method is a method just like any other method defined by us. Suppose you make a method to add two numbers: add()
2) U know the name of the method and can call it like add() and the program flow goes to add() method.
3) Just think what happens when we excute the Java Program.
4) We invoke the JVM and pass the name of the class to run
5) What will JVM do? It will create an instance of the class we passed to it and will call a method "main()". Thus There must be an entry point so that a program can begin executing and in Java such a method from which the execution will start is named as "main()" method. After all JVM must know the method which it should call to start program execution...
Hope this helps
Regards
GC
-----------------------------------------------------------------------------------SadaMy friend asked me this question.!
Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?
Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..
CAn anyone tell me the reason?
And correct me If am wrong?
You can run java program without main method with the help of static block
Because it executes as soon as class loaded.
Also memory is initialized to static block at the time of class compiled
But still we require main() method because static block is called automatically whereas we can call main() method as on our require meant also to take command line argument we need main() method
Is it so? 😕ianoopStatic block trick, no more applicable for JDK 1.7+😏
if a static block is present in the program,then jvm execute it first,and then search for main method...
though if main method is not found..it gave an error..bt that we can eliminate by using system.exit inside block method
// SAVE IT Demo1 and this is successfully run my frd
public class Demo1
{
static
{
System.out.println("yeah sure painkiller");
System.exit(0);
}
}
New version of java doesnot allow executing program without main()
but hw we can run this program??????Sadaguys..! My friend told me that we can write and run a java program without using the main method.
i.e By using static
she gave this example program...!
public void static( )
public class
{
public void static( )
{
System.out.println( "Without main method" );
System.exit(0);
}
}
please go through the above posts. You will find the explanation.Ankit Niranjanbut hw we can run this program??????
In java program ,the program execution starts from main method.But in java program all code must be declared in class. to access class we have to create an object for this.But to start execution all of this we have to execute main method .so have declared class as public static in which main method is saved.and we declare this class as file name to access direct main method without creating object of class in which the main method is saved.SadaMy friend asked me this question.!
Do a java program runs without the main method? If NO, why? What is the need of main method there in the java program?
Guys..! tell me the answer. I dont know the reason, but I know that java program must needs main method..
CAn anyone tell me the reason?
And correct me If am wrong?
My question is that is there any specific problem field(s) to skip the main function and keep the program running using static block as stated above; because as per my view, we cannot have much of the versatility and complex functionality with the ystatic block as with the main function.
So is there any special field of using static block like this?
If you want to execute a program you must include main method.if main method is not included in the program then the program will not execute and javac will give error.and there is nothing to replace the main method.you have to include main method becoz main method is the reason of execution .you cant skip it with any other thing.Deepika BansalGreat information friends. Thank you all.
My question is that is there any specific problem field(s) to skip the main function and keep the program running using static block as stated above; because as per my view, we cannot have much of the versatility and complex functionality with the ystatic block as with the main function.
So is there any special field of using static block like this?
I tried the above code in ecllipse..bt its showing error as follows
so plz hlp me out..
Check the following reply in this thread..madhuri thallurihello friends..
I tried the above code in ecllipse..bt its showing error as follows
so plz hlp me out..
Anoop KumarStatic block trick, no more applicable for JDK 1.7+😏
Yes, Java program can run without main method:
You have to use static blocks like :
public final class Test {
static
{
System.out.println("FIND");
} }
o/p: FIND
This works with all the versions before JAVA 7.
Yes of course it is possible. You can use static blocks for that. Just put the code in the static block and execute it. The process by which java compiler executes the program is as follows:
- JVM loads your class.
- After arranging all the blocks and it runs static block.
- It then finally stirs across the main() method and it uses it.
But for directly running through command line using "javac" its not possible. As JVM uses the main method to execute your program through command line and it can't find one. so its not possible in command line but possible via static block while running from idle.
Yes, by using static blocks we can write a java program without main method.But the point to be remember is that is this works with all the versions of before Java 7.
Related Posts
@Nandan Rao · Jul 23, 2014
@Kaustubh Katdare · Aug 13, 2015
@n.kirubhakaran · Oct 27, 2012
@Kaustubh Katdare · Aug 6, 2013
@jbozee · Feb 13, 2009