0

why "main()" must in every java programm? i had run java program without main() by using static method only...

java course module1 question no. 2 they are saying correct if you click on option which says main() is must in every java programm...

8th Jun 2019, 4:47 AM
kamlesh patil
kamlesh patil - avatar
22 ответов
+ 1
~ swim ~ i have done it in java 6...
8th Jun 2019, 8:10 AM
kamlesh patil
kamlesh patil - avatar
+ 1
~ swim ~ yeah i got that warning whenever i executed my code...but at the end we can still execute program without main()...even though warning is at the end of complete execution of program on console...
8th Jun 2019, 8:19 AM
kamlesh patil
kamlesh patil - avatar
0
~ swim ~ share information about that if you got reason for that warning...and other ways of running program without main()...
8th Jun 2019, 8:45 AM
kamlesh patil
kamlesh patil - avatar
0
Here is the deal: if you don't have a main(), then where the hell will your program start? main() is not an obligation just to annoy you. It is mandatory because it is the only mechanism that lets the system know where the program starts. If you've run programs where you didn't have to make a main() yourself, there are two possibilities: 1: you used a framework that provides a main() for you, and uses other rules as for where things start (possibly it doesn't even matter what goes first or last). Examples: JavaFX, Java applets, Java webapps. 2: you used Java jshell, the REPL code testing tool. It executes the things you type to it, when you type them to it. Things start with the first stuff you type.
8th Jun 2019, 10:25 AM
kumesana
0
kumesana i already successfully executed program wihout any error except the warning which comes after entire completion of program execution... i will suggest you to read conversation of ~ swim ~ and mine you will get to know what we already discussed...
8th Jun 2019, 11:08 AM
kamlesh patil
kamlesh patil - avatar
0
~ swim ~ you are right now i can't able to run program without main() it is giving Error: Main method not found so and so...It was on windows OS when i tried to run it without main and it's 1-2 year ago...but today tried same code in ubuntu 18.04 on terminal...also in visual studio code but it's giving Error... kumesana there was a time when in was possible to run without main but not now...
8th Jun 2019, 11:20 AM
kamlesh patil
kamlesh patil - avatar
0
~ swim ~ Windows-7 java6 in my laptop...
8th Jun 2019, 11:31 AM
kamlesh patil
kamlesh patil - avatar
0
There was no such time and nothing has changed. Absence of main() isn't something to "warn" about, absence of main() means absence of a starting point and therefore of any ability to run. I already said that, it is the truth and it always was. I understand that you were once mistaken about an ability to run without main(), but I am going to bet you are unable to tell me how did the system decide where to start? Most likely, there was a main(). It didn't leave you an impression, or it was hidden from you by an existing framework you were using. Or you were not even using Java. Whatever it is, this past thing you're talking about never was a thing.
8th Jun 2019, 11:38 AM
kumesana
0
kumesana don't worry you are not the one who didn't believed on me untill i prove this thing... ok here is reason why we can able to write without main() that time (i don't know why it's not working today)... Explanation:- why we write public static void main() in java instead of only public void main() because the thing is in any class static method got served first in order they are written that's why if you write code like this class classname{ static anyMethod(){ //defination } public static void main(){ //defination } } here program will execute static anyMethod first after that it will send controls to main()... But today i written same code and it's executing main() first not that first static method so i can say yes you are right...
8th Jun 2019, 12:37 PM
kamlesh patil
kamlesh patil - avatar
0
~ swim ~ printing statement only and it worked i know that it's against the java rules but it worked...and also as you said earlier yes there may chances that windows might have old thing because i developed project in java-6 and java-8 but there might be chance while i am learning java i didn't upgraded it and version might be old...and i remember the version on which i had developed my projects...
8th Jun 2019, 12:48 PM
kamlesh patil
kamlesh patil - avatar
0
The things you're thinking of are static *blocks*, not static *methods*. Yes you can get them to run before Java notices that there isn't any main() method in the class you asked to run. However, while the code is run indeed when you do that, (at least it probably still is), it requires quite some leniency to claim that "it works" and "you can" do that. After Java finishes loading the class and notices there is no main() it displays a huge error and makes it very clear it went wrong. It ruins the output and returns a system error that would immediately stop any script that cares about programs running without errors. Not a normal operation, not silently failing, it is not normal to do and Java is making that very obvious. Anyway the code was like that: public class WithoutMain { static { System.out.println("Hello"); } }
8th Jun 2019, 12:48 PM
kumesana
0
kumesana yeah sorry i said method instead of block...
8th Jun 2019, 12:53 PM
kamlesh patil
kamlesh patil - avatar
0
~ swim ~ kumesana just now i made search about this and most of the source like stackoverflow many contributors are saying yes we can but upto java-6 only after that version it wouldn't work...
8th Jun 2019, 12:55 PM
kamlesh patil
kamlesh patil - avatar
0
Well as I said claiming that "it worked" requires a lot of leniency and delusion when facing the evidence. So it would make sense that later versions of Java would try and detect the situation to reject it and tell you "no. That is *not* going to work. Maybe you can't tell the difference now, but when you will finally be able to, we cannot risk that you grew the habit of thinking you don't need clearly indentified main()"
8th Jun 2019, 1:03 PM
kumesana
0
kumesana ok...
8th Jun 2019, 1:07 PM
kamlesh patil
kamlesh patil - avatar