Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

private static void main

  Asked By: Muaz    Date: Oct 07    Category: Java    Views: 4600
  

Here is the code and it works fine..!!! As written in books & my understanding
it should not.

Pl tell me the reason behind it...!!!

public class Test
{
private static void main(String args[])
{
System.out.println(" Hello World..");
}
}

Share: 

 

10 Answers Found

 
Answer #1    Answered By: Tommy Thompson     Answered On: Oct 07

It did not work.
The message was "Main method not public."

However it did compile without any errors.

 
Answer #2    Answered By: Adelmo Fischer     Answered On: Oct 07

So what is your problem?
The JVM requires a method with the following syntax
public static  void main(String[] args)
to be present in your class in order to be able to run this class.
Of course you can also have a method which is private. There is nothing
wrong with it.
Why should the compiler complain? Since the method signature does not match
the requirements
of course this class cannot be executed.
I do not understand what your problem is. You get the error that you deserve
when you try to run your
class.

 
Answer #3    Answered By: Mansur Bashara     Answered On: Oct 07

it dosent work at any cost....
its showing run time error....

 
Answer #4    Answered By: Farah Khan     Answered On: Oct 07

I don't know about this, but you seem to have been
able to both run and compile, at least you say so,
without having the right declarations for the main()
method.

The reason why the main() method should be public, is
so that you can access it, that is starting the
application. It can't be instantiated either, so
therefore it's static. An application should not
return anything, therefore it's void.

Now, if you made this hack work, it's probably
beacause you're compiling/running it with an earlier
version of Java, I don't know how early it has to be
though, but it may work with 1.1. In C++ for example
you have the main() method declared as int and you
don't write public static  and the rest, but I read (I
believe in Java Language Specification from Sun) that
when they "revamped" C++ into Java, which is how Java
is explained as being in the earliest Java books, they
corrected these errors. So, don't worry about how the
hack might work, the same "error" works  in C++ all the
time.

I want to thank the Java creators for crating a much
more perfect language as java has a lot more elegant
debugger than its' predecessor and the just for Java
invented garbage collector.

Happy programming and I really recommend the
downloadable book I mentioned above.

 
Answer #5    Answered By: Eline Bakker     Answered On: Oct 07

public class Test
{
private static  void main(String args[])
{
System.out.println(" Hello World..");
}
}

This code  compiles fine  on any j2sdk using the javac command, but this
code will never be interpreted correctly by any of the standard JVM (
one provided by sun )
The only case when the compiled code (Test.java) can run is when you are
running some other JVM (like kaffe and lot more like that ) To find out
which JVM you are running see the results of "java -version"

So in the last --- the motto of the whole story.. stick to the standards

 
Answer #6    Answered By: Harriet Hughes     Answered On: Oct 07

change private  to public in this line.

> private static  void main(String args[])

and it will work.

 
Answer #7    Answered By: Blandina Garcia     Answered On: Oct 07

If you have reported what is the exact message in the
runtime error, someone might have helped you with it.

Now you will have to write another mail to mention that.

Please try to fill as much relevant information in a single
post as possible.

 
Answer #8    Answered By: Addison Campbell     Answered On: Oct 07

The main() method has to be public because a PRIVATE method can not access the
PUBLIC class and PUBLIC class members and member functions as a lower access
specifier cannot access the one higher to it whereas vice-versa is possible.

 
Answer #9    Answered By: Aaleyah Khan     Answered On: Oct 07

Sorry, this doesn't make sense to me. A private  method can quite happily access
public classes and their public members.

It is the reverse which is true - a private method cannot be accessed by other
classes (only from within its own class).

This makes "main" inaccessible outside "Test" and therefore in theory
inaccessible to the run-time environment (although I can imagine some run-time
environments not bothering to check for this).

 
Answer #10    Answered By: Marta Kim     Answered On: Oct 07
 
Didn't find what you were looking for? Find more on private static void main Or get search suggestion and latest updates.




Tagged: