Can Java program run without the Main method?
My 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?
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?
Replies
-
faizaanRe: Do a Java program runs without the Main method?
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. -
Manish GoyalI think main() method is must in java ...
since java is pure object oriented language....and also main() method is an entry point...through which execution of program begins............ -
sarveshgupta@pallavi: Faizan is right it is possible to have a program to start execution without entering into main first.
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 -
Sahithi PallaviThanks a ton guys...!
So, main method is must to run a java program...! -
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);
}
} -
Manish Goyal
ππππ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 -
sarveshgupta@Pallavi: try the code yourself as I am doubtful this program will run.
And I told you that Java Applet programs don't generally have the main method.
they have paint() and init() method. -
ms_csThe java program can run without main method with this static method
-
sarveshgupta
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? -
Manish GoyalYeah i am totally confused with this...where is the name of function
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?? -
sookieIf 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:\"
/** * * @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. π -
Manish Goyalthanks sookie ........for clarifying this concept
-
ms_cssookie: you are right..nice explanation
-
accoolaryanyes we can run a java program without a main method .as main method is necessary but if we use a block named static ,every statement in it will be executed without main
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);
} -
rama_krish627107627@ccc108:~> javac crazy1.java
crazy1.java:1: 'class' or 'interface' expected
public void static( )
^
crazy1.java:2: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. -
sarveshguptaya nice explanation sookie
-
sookie
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: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. -
techno_ishantjava program can run without main()
For Example APPLET...π -
rahulgakharya u can run a java program without main
try this
public class A
{
static
{
System.out.println( "Without main method" );
System.exit(0);
}
} -
naveen029as you know,java achieves platform independency. so it uses Java Virtual Machiene(JVM) for platform independency.so java developer also connect main() method to this jvm.
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. -
rahulgakharbut my friend told me we can run without main..............
bt he doesnt know how........... -
Sahithi Pallavi
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. -
Amit PorwalRe: Do a Java program runs without the Main method?
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.
-
crazy_rajaclass Hello
{
static
{
System.out.println("This is the Java Program That Runs Without Main Method");
System.exit(0);
}
}
Try this one....it works -
Ricky008
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. -
karan20mif we dont write main() in the progrm then the compiler doesnt know where to start the program..
-
gayathri anbui have tried this.. it is not executing successfully..
-
gayathri anbuYou can write a runnable program which does not have main method at all. During run time jvm will search for the main method after exiting from this block. If it does not find the main method, it throws an exception. To avoid the exception System.exit(0); statement is used which terminates the program at the end of the static block itself.
[TABLE]
[TR]
[TD]class MainMethodNot
{
static
{
System.out.println("This java program have run without the run method");
System.exit(0);
}
}[/TD]
[/TR]
[/TABLE]
-
karthikswapna1yes of course the bellow program is executingSadaguys..! 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);
}
} -
vinciin reality this program wont work .my compiler gives me an error saying that main method not defined
-
Sahithi Pallavi
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. π -
jeklin110[TABLE]
[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.
----------
#-Link-Snipped-# -
jeklin110Hi
Welcome to you good question asked by your friends i am finding answer for you..
--------- -
balaji MCAYes We can run the java program with out main method using static class name alone is enough to run a program in java sum operation only needs main clause
-
csmentorHi,
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. -
greatcoder#-Link-Snipped-#
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 -
priya.123
-----------------------------------------------------------------------------------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 -
Anoop KumarStatic block trick, no more applicable for JDK 1.7+π
-
Sahithi Pallavi
Is it so? πianoopStatic block trick, no more applicable for JDK 1.7+π -
ashieyes sada ...we can run java program without main...by using static block
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 -
KACHA PIYUSH// PAIN-KILLER SOLUTION OF ALL LOGIC PROGRAM
// SAVE IT Demo1 and this is successfully run my frd
public class Demo1
{
static
{
System.out.println("yeah sure painkiller");
System.exit(0);
}
} -
surazProgram without main() used to run earlier,as static block is executed first and within static block you write sop and then terminate using system.exit(0).
New version of java doesnot allow executing program without main()
in jdk 1.7(build1.7.0-ea-b85),It gives run time Exception.Check it outβ -
Sahithi Pallaviyeah, so is there no chance to run a program in jdk1.7 without main()?
-
Ankit Niranjan
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);
}
} -
Sahithi Pallavi
please go through the above posts. You will find the explanation.Ankit Niranjanbut hw we can run this program?????? -
Extreme JavaYes System.exit() is the right thing to do at the end of static block.
-
Rashmi Yadav
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? -
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? -
Rashmi Yadav
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? -
madhuri thallurihello friends..
I tried the above code in ecllipse..bt its showing error as follows
so plz hlp me out.. -
Anoop Kumar
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+π
You are reading an archived discussion.
Related Posts
as my system after entering password it will start after 10 to 15 min why it is so ???? please any one help me out in this problemππ
Source: Wolfram|Alpha PR Team
We're pleased to announce that Wolfram|Alpha is at the Web 2.0 Expo. The event takes place November 16-19, 2009, at the Javits Center in New York,...
It is request to all the ceans who visit my website ..Please enter your crazy engineers user name ....
and also Please put your problems in this site..i think your...
Times of India reporting:-
The number of students from India enrolled in US universities and colleges crossed 100,000 for the first time ever this year even as international enrollments in...
I'm thrilled to see what this guy is up to -
Pranav Mistry: The thrilling potential of SixthSense technology | Video on TED.com
How can this technology be developed further?