• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

main method significance

 
Ranch Hand
Posts: 441
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
main() method is defined as: public static void main(String a[])


It is declared as public so that it can be called outside from the class. What is the meaning of this? How it is actually being called by JVM? Does JVM call it from some class?

Why it is declared as static? The JVM could have called it using an object as well, isn't it?
 
Rancher
Posts: 1171
18
IntelliJ IDE Hibernate Firefox Browser MySQL Database Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The public keyword is an access specifier, which allows the programmer to control the visibility of class members.
When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared.
In this case, main must be declared as public, since it must be called by code outside of its class when the program is started.
The keyword static allows main to be called without having to instantiate a particular instance of the class.
Without having declared main method static, your program will successfully compile but won't execute and report error at run time.
This is necessary since main is called by the Java interpreter before any objects are made.
The keyword void simply tells the compiler that main does not return a value.
The main is the method called when a Java application begins.
Keep in mind that Java is case-sensitive.
Thus, Main is different from main.
It is important to understand that the Java compiler will compile classes that do not contain a main method, but the Java interpreter has no way to run these classes.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:Without having declared main method static, your program will successfully compile but won't execute and report error at run time.
This is necessary since main is called by the Java interpreter before any objects are made.
...
The main is the method called when a Java application begins.
...
It is important to understand that the Java compiler will compile classes that do not contain a main method, but the Java interpreter has no way to run these classes.



Not quite, though this is not something I would suggest actually doing as anything other than a bit of fun, and I'm only posting this here as this is the Java General forum and not Java Beginners.

Java does load the class object for the class it is trying to run, running the above (java test.NoMain) will result in NoMain being loaded.
As part of being loaded the static code block is executed.
You do get an error, though, when it (finally) goes off to look for the main() method.
 
Marshal
Posts: 73979
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:. . .  Java does load the class object for the class it is trying to run, running the above (java test.NoMain) will result in NoMain being loaded.
As part of being loaded the static code block is executed.

Not any more. That trick ceased to work in Java7. You get an exception about no main method before the initialiser runs nowadays.

You do get an error, though, when it (finally) goes off to look for the main() method.

In the days when that trick did work, people followed the statements with System.exit(0); to prevent any error messages.Using JDK9u4Class members not marked public are accessible from other code in the same package; public access allows it to be run from outwith the package.
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah.
I did test it, but it's IntelliJ being too clever by half.

It's own launcher allows it to happen.
 
Campbell Ritchie
Marshal
Posts: 73979
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe you should report that to JetBrains; it doesn't sound correct. It means, if you are enough of a wally to write that sort of code, you can have code which runs on IntelliJ and won't run on the command line.
 
Saloon Keeper
Posts: 24283
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting.

The original implementation was that the JVM would load the indicated main class (as given on the "java" command line). That would cause the static initializer code to execute. Then, once the class had loaded, the JVM would introspect to find the static main(string[])) method and invoke it.

What Campbell has said indicates that the JVM now has a way to introspect for the main method before actually "loading" the class. This is a plus in that a defective app wouldn't do possibly harmful things before being formally started (via call to main()), but it would be at odds with the previous algorithm and thus it's likely that a lot more programs than just IntelliJ would be "broken" that way. Basically any legacy code that called main() instead of spawning a new (remote-debug) JVM would have the same symptoms.

It would be interesting to see if there are new features in java.lang.Class that deal with this.
 
Tim Holloway
Saloon Keeper
Posts: 24283
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Update: https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.9

Actually, I'm not sure how to read this. It seems to imply some weird things, including multiple class-initialization internal methods.
 
Campbell Ritchie
Marshal
Posts: 73979
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My, that is interesting stuff.

That explains where the mysterious <init> which substitutes for the constructor comes from. It also tells you that it isn't a normal method because you can only call it on the object before it is instantiated. It also mentions the <clinit> method and tells you that <clinit> is never used. It also mentions polymorphic methods of which there are only two in the entire Java® Universe. I couldn't actually find the main method in that section.
 
Tim Holloway
Saloon Keeper
Posts: 24283
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's actually quite abstract. I had no problem with <init> and <clinit>, since back when I was putting together one of the first PC-based C++ development systems, I'd had to invent one of my own. Which is why I tend to forget and call <init>() as "_init()". Thats the name I was kludging into my generated code. Java doesn't have to be so kludgy, since its compiler and runtime are in cahoots to support illegal method names, but only internally.

This section actually details how the JVM must work, and it's basically saying that under post Java-7 rules, a <clinit> method will only be executed IF the method has its "static" attribute set, whereas before, the mere existence of a <clinit> would get it executed. And then gets all confusing referencing multiple <clinit>'s.

I know of no way that javac is ever going to create multiple <clinit> methods, but perhaps this is a nod to other non-Java compilers producing classfiles for a JVM to run. There can, of course, be multiple <init> methods, because that's just the internal name assigned to the constructor method, and you can define multiple constructors having different argument signatures.
 
Bartender
Posts: 2235
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Ah.
I did test it, but it's IntelliJ being too clever by half.

It's own launcher allows it to happen.



I just tested it on IntelliJ Community Edition 2017.3 and JDK 1.8.0_152 and I got:

Error: Main method not found in class baczynsp.Hello, please define the main method as:
  public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They must have fixed it then.
Looking at mine here at work and it's a 2016 IntelliJ.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic