Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Main

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just done a mock test and one of the questions was if main can be native, it says it can. I thought because native methods cant have bodies that this would rule out main.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the JLS, §12.1.4 Invoke Test.main, these are the requirements for the main method:


The method main must be declared public, static, and void. It must accept a single argument that is an array of strings.


Notice that it doesn't say anything about where the body of main is defined. Therefore, it can be native. Note that if main is declared native, it still has a body, it just isn't defined as most method bodies are because it is native.
I hope that helps,
Corey
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will it work?
class A
{
private static void main(String args[])
{
System.out.println("Hello world");
}
}
?? why
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Richard Selva:
Will it work?
class A
{
private static void main(String args[])
{
System.out.println("Hello world");
}
}
?? why



Oops, it did work.
But JLS states main method should be declared public, static and void?
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declaring main as private is ok....but it is not ok for the starting point. If you compile it no problem...but run time there will be an error
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare main to be whatever you want - private, final, native, whatever. The rules that state that main must be public, static, and take an array of Strings only apply to the main method that you intend to use as an entry point. The JVM will invoke the method that matches that description. The main method you defined above (private) is perfectly legal, but it can't be used as an entry point as the JVM won't have access to invoke it. If you were to try, I'm guessing you'd run into a NoSuchMethodException.
I hope that helps,
Corey
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried this:

And it compiled and run without exception ??? I though thi swasn't allowed ???
 
brent spearios
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
works fine in JDK 1.1 through 1.3 that I tested. *Nothing* needs to be public after
all in any JDK below 1.3!
Its another of javas many bugs
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually this works..as main is like any other method in a class,giving it a private , protected
wont generate any kind of CE or RE.
But for the exam point of view we need to follow what JLS says ie.

"The method main must be declared public, static, and void. It must accept a single argument that is an array of strings"
Swapna
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm...I'm not really sure why that works. According to the JVMS, §5.2 Virtual Machine Start-up:


The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (�5.3.1). The Java virtual machine then links the initial class, initializes it, and invokes its public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java virtual machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.


and from the JLS, §12.1.4 Invoke Test.main:


Finally, after completion of the initialization for class Test (during which other consequential loading, linking, and initializing may have occurred), the method main of Test is invoked.
The method main must be declared public, static, and void. It must accept a single argument that is an array of strings.


Of course, none of this says what happens in the cast the main method doesn't meet these requirements. As far as the exam goes, however, remember that main must be public, static, and take an array of Strings.
Corey
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not work (run) in 1.4.0.
Error message is:
Main method not public.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by R Arun:
It does not work (run) in 1.4.0.
Error message is:
Main method not public.


This is exactly the behavior I would expect. Thanks, R.Arun.
Corey
 
    Bookmark Topic Watch Topic
  • New Topic