Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Newbie query regarding Java

Thread view: 
abhishek.raju@gmail.com - 08 Feb 2007 09:37 GMT
Hi All ,

I have the following query :-

If I define the main method as a Private , then run the demo program
the program obviously doesnt run ,we have a runtime error .What
exactly is the reason behind it .

I know its not a good programming practice and the main should always
be in Public.
But I want a conceptual answer as to why its not a valid thing to make
the main as Private.
Any help would be highly appreciated .

Regards,
Abhishek.
Michael Rauscher - 08 Feb 2007 10:08 GMT
abhishek.raju@gmail.com schrieb:
> Hi All ,
>
[quoted text clipped - 3 lines]
> the program obviously doesnt run ,we have a runtime error .What
> exactly is the reason behind it .

The JVM looks for a public static main method that takes a String array
as parameter.

> I know its not a good programming practice and the main should always
> be in Public.
> But I want a conceptual answer as to why its not a valid thing to make
> the main as Private.
> Any help would be highly appreciated .

It's specified to work this way, e. g. have a look at [1].

Not focusing the specification: public methods may be called from other
classes in other packages, e. g.

OtherApp.main(new String[]{});

Of course, one can use reflection to generalize this.

Bye
Michael

[1]
<http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.1.4>
Nigel Wade - 08 Feb 2007 11:13 GMT
> Hi All ,
>
> I have the following query :-
>
> If I define the main method as a Private ,

private, it doesn't have a capital letter.

> then run the demo program
> the program obviously doesnt run ,we have a runtime error .What
> exactly is the reason behind it .

Because a private method is only visible in the class in which it is defined. So
a private main() method is not visible to the JVM so it cannot invoke it.

> I know its not a good programming practice and the main should always
> be in Public.

It's not that it's bad programming practice, it just won't work.

> But I want a conceptual answer as to why its not a valid thing to make
> the main as Private.
> Any help would be highly appreciated .
>
> Regards,
> Abhishek.

Signature

Nigel Wade, System Administrator, Space Plasma Physics Group,
           University of Leicester, Leicester, LE1 7RH, UK
E-mail :    nmw@ion.le.ac.uk
Phone :     +44 (0)116 2523548, Fax : +44 (0)116 2523555

Matt Rose - 08 Feb 2007 15:13 GMT
On Feb 8, 9:37 am, abhishek.r...@gmail.com wrote:
> Hi All ,
>
[quoted text clipped - 12 lines]
> Regards,
> Abhishek.

The idea behind the private modifier is that, in general, the member
is only visible within your class: you want to keep it totally under
your control because it's some sort of internal method or property
that the outside world has no business fiddling with.

The point of a public method is that you're exposing an interface
(with a small 'i') to the outside world that anyone can come along and
interact with. You don't get much more "outside world" than the
command line.

Technically, any modifier could have been specified but "public" fits
more with its use.

(Aside.) Whether the convention of having a method with a magic
signature as the entry point into all standalone java programs is
better than some more explicit approach like an Interface might be
worth wondering about. It made the original sales case for java closer
to C I suppose. I expect other people can come up with better
arguments either way.

Matt
Oliver Wong - 08 Feb 2007 18:34 GMT
> (Aside.) Whether the convention of having a method with a magic
> signature as the entry point into all standalone java programs is
> better than some more explicit approach like an Interface might be
> worth wondering about. It made the original sales case for java closer
> to C I suppose. I expect other people can come up with better
> arguments either way.

   From my perspective, it's not that method which has a magic signature:
the evidence is that you can have multiple class each with their own "public
void static main(String[])" methods, and none of them are any more magical
than any other. Rather, it's the behaviour of the JVM itself which is
"magical". It can somehow manage the transfer of control from the command
line (or other external environment) into the JVM.

   It just so happens that the "java.exe" command happens to take a class
as a command line argument, and tries to invoke the static main(String[])
method of that class. But this is not conceptually different from a browser
creating an instance of a JApplet, and invoking its init(), start() and
stop() and destroy() methods. Doesn't mean that the JApplet is magical. It
just means that there's some sort of transfer of control from your program
to the outside environment.

   I believe there are even IDEs for which you can specify the entry point
to be something other than a method called "main" (e.g. a static method
called "foo(String[])" instead), which further reinforces that the magic
lies not in the method, but somewhere at a higher level than that.

   - Oliver
Karl Uppiano - 09 Feb 2007 03:22 GMT
>> (Aside.) Whether the convention of having a method with a magic
>> signature as the entry point into all standalone java programs is
[quoted text clipped - 22 lines]
> called "foo(String[])" instead), which further reinforces that the magic
> lies not in the method, but somewhere at a higher level than that.

There is nothing "magical" about it at all. It is called a "standard
interface" -- a publicly documented protocol that everyone agrees on (or
must accept even if they don't agree).

   public static void main(String[] args) {...}

is the standard entry point for all Java applications.
Chris Uppal - 09 Feb 2007 16:22 GMT
> There is nothing "magical" about it at all. It is called a "standard
> interface" -- a publicly documented protocol that everyone agrees on (or
[quoted text clipped - 3 lines]
>
> is the standard entry point for all Java applications.

Not really true.  It's the way that the specific launcher /for/ Java
applications called (on Windows) java.exe, or javaw.exe is written.  But there
is nothing blessed about that launcher -- anyone can write their own which uses
any entry-point they like, or no enty-point at all.

   -- chris


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.