Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

WHy does main() work with private accessibility?

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Main
{
private static void main(String[] args)
{
System.out.println("Hello World MAIN!");
}
}
class Main1
{
public static void main(String[] args)
{
System.out.println("Hello World Main1!");
}
}
class Main2
{
public static void main(String[] args)
{
System.out.println("Hello World Main2!");
}
}

SURPRISE::: The output of the above source file is
Hello World MAIN!
Can anyone please explain ?
Bhaskar
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The main() method is declared public by convention...it works even if it private....but the requirement is that it has to be static so that it could be the entry point for the java applications.
Rashmi
 
Enthuware Software Support
Posts: 4850
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this out: http://www.jdiscuss.com/Enthuse/jsp/QuestionADay.jsp?date=2001-07-10
-Paul.
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sun considers it a feature. They have no intention of fixing it.
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Well, I was also stumped when I first saw this output.But, if we think about it, access modifiers (private,public,protected) signify the access of the method in our outside the class.However, these modifiers doesnot effect the call to the methods in any case.So in the present class context, using any access modifier should not make a difference!
May be this is what Sun also believes in!
-- Sandeep
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I agree with Sandeep that the accessibility of main is not imp in this case. The simple reason as Sandeep stated accessibility features are access by & within classes. main is designed to be static function that is called by the jvm when the appropraite source file is run.ie the class with the same name as the file.So accessibility is of no consequence.However consider a public subclass with no main & a superclass with a private main & it will be amply clear why sun desires that main should be public.
Regards,
kaushik
 
Ranch Hand
Posts: 2379
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Pals,
when you run the program with -----
java Main
the output will be : Hello World MAIN
java Main1
the output will be : Hello World Main1
java Main2
the output will be : Hello World Main2
There should be no problem here.

------------------
azaman
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this code snipplet and let me know you comments on it :

Now run Sub by using java Sub in the DOS prompt.What do you think should happen?
-- Sandeep
SCJP2,OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Desai Sandeep:
[B]Try this code snipplet and let me know you comments on it :

Now run Sub by using java Sub in the DOS prompt.What do you think should happen?
-- Sandeep
SCJP2,OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)[/B]


It seems to be executing the line

??!!

------------------
Antti Barck
It Solutions Consultant, NSD Oy
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Antti,
Why is this happening?
Since the main method is defined with a private modifier, it would have meant that it is not getting inherited.
However, the result says that the main method is infact getting inherited.
However, I proved my inference wrong when I tried this piece of code snipplet :

If you compile the above code snipplet you would get the following error:

MainTester1.java:13: No method matching main(java.lang.String[]) found in class MainTester.MainTester.main(s);
^
1 error

This means the private main is not getting inherited.If that is the case, how is the program running with privately declared main in the Super class?
Comments, please!
-- Sandeep
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But isn't the main method supposed to reside in that class which has a Public access modifier.. , if both classes have no modifer it' o.k.
~~~~Just a thought~~~~~
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandeep!
Is this sorts of a SDK versions specific issue?!
When I throw following code to SDK 1.4:

It will bot compile and run ok. We should assume that less restrictive access (protected,public) should work for main too. Lets make it private:

Javac tolds me that:

Isn't this something that happens in inheritance as well?

Well,

??!!
(Am I getting too far from the original issue?)
------------------
Antti Barck
It Solutions Consultant, NSD Oy
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Sandeep...
Have you got the answer to your own question?
It's an interesting one, and quite frankly to say that I don't know why it behaves like that..
I've tried the following code:

When I run "java X", it gives me Z as the output..
It seems like JVM can find the "private" and "unsupposedly inherited" main() method.
Could you give me some light here?
or anyone else??
- eric
 
Bhaskar Selvaraju
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,
Seems like i opened a pandora's box here.
Well i guess are all gaining something here. But sandeep's question is a good one. Can someone plz explain.
Bhaskar
------------------
\```/
(o o)
---oOO--(_)--OOo---
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!
I think that what happens here that JVM wants to find the signature static void main(String[] x) in the class it is going to execute. In the exam there may be question about this and you should then keep in mind the "de-facto" signature which is, of course, public static void main(String[]).
I think that reasoning this out does not lead us anywhere and, IHMO, it would be as fertile as figuring out what happens before main() in C is called.
------------------
Antti Barck
It Solutions Consultant, NSD Oy
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I really don't have an answer to my question!
I can only say that private static main(String[]) is inherited in some mysterious way, although you cannot explicitly call the main in your subclass.
Will keep you informed if I find any further information.
-- Sandeep
[This message has been edited by Desai Sandeep (edited July 26, 2001).]
 
Rashmi Hosalli
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
According to me:
1.The "private static void main(String[] args)" in Super class will be inherited by the Sub class ,just like any other method....but,it will only be executed or in other words be considered as the entry point by the JVM if the derived class has no main method.
2. If both the base and the derived classes have their own main methods and you want to execute the derived class,then the main method of the derived class will be executed and not that of the base class even though it would have inherited that method.
3. Another point is,main method can have any of the public,private or protected accessibility....but the main method of the derived class should take care not to restrict the accesibility of the main method it has inherited from its parent....in other words we are just overriding the main method in the derived class.
Hope I am right.Do let me know.
Rashmi
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



1.The "private static void main(String[] args)" in Super class will be inherited by the Sub class ,just like any other method....but,it will only be executed or in other words be considered as the entry point by the JVM if the derived class has no main method.


It is to be inherited like any other method, the this main method has private accessibility and hence cannot be inherited.
I don't get how this is happening..Can anyone pls. explain this!
Aakanksha
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!
My opinion is still that this issue does not belong here.
This feature deals with unknown source of caller of the main method!
I think that it is good practice to keep the main method signature in its "de-facto" form:
public static void main(String[] args)
There is no benefit trying to figure out why java has some irrugularities on this specific issue.
------------------
Antti Barck
It Solutions Consultant, NSD Oy
 
Rashmi Hosalli
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aakanksha,
I know that private methods are not inherited by the derived classes...but I guess in case of the main method its different!
If you try Sandeep's code
//file = Sub.javaclass Super
{
private static void main(String[] args)
{
System.out.println("In the Super class");
}
}
public class Sub extends Super {}
you'll see that the main method is inherited even if it is private,public or protected and the compiler compiles and runs the pgm.But if you do not provide a main method in the super class,you'll get the run time NoSuchMethodError as the compiler cannot find the main method....So,it is clear that the main method gets inherited no matter what access modifier it has!!
Rashmi
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rashmi,
I guess we need to use some other word for this behaviour than saying it is "inherited".Because, had the main method been inherited, you should have been able to invoke main from any static method of the subclass.
Try doing this, and you will get an error stating that no matching main(String[] args) found!Refer to my earlier post for the code snipplet.
HTH,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited July 28, 2001).]
 
Rashmi Hosalli
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandeep,
I agree that "inherited" is a wrong word to use here...but then I couldn't find another word
Anyway,you said:
had the main method been inherited, you should have been able to invoke main from any static method of the subclass.
Try doing this, and you will get an error stating that no matching main(String[] args) found!Refer to my earlier post for the code snipplet.
What about something like this:

class Super
{
public static void main(String[] args)
{
System.out.println("In the Super class");
}
}
public class Sub extends Super
{
static void method()
{
String[] myarr = new String[2];
main(myarr);
}
}
I have a few things to say here:
1. This compiles and runs fine printing out "In the Super class" provided main method in the Super is not private which is obvious.
2. Now,if I want to execute the method() in Sub,I can do that in the main method and since we are not interested in Sub having the main method,I'll have to do so in the main method of the Super which will lead to other problems!!!
3. So,the compiler accepts calling the main method in method() of Sub class,provided it is does not have private access.
Let me know if I have moved away from your point
Rashmi
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rashmi,
I was commenting in context of private static void main(String[]), where we can not say if the main method is inherited or not.
As we discussed earlier, on one hand, it seems to be inherited, when we donot have a main in the subclass, but run the subclass using "java subclass".
On the other had you can not say it is inherited because, if you try to invoke the main in the subclass, just as you did with the public declared main, you would get a compile-time error!
There surely has to be more to this, what we cannot see at the moment.Will keep you informed if I happen to make some progress on it.
Thank you for your views,
Sandeep
Sun Certified Programmer for Java 2 Platform Scored 93%
Oracle Certified Solution Developer - JDeveloper
-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java Scored 56 out of 59
-- Object-Oriented Analysis and Design with UML Scored 73%
Oracle Certified Enterprise Developer - Oracle Internet Platform
-- Enterprise Connectivity with J2EE Scored 72%
-- Enterprise Development on the Oracle Internet Platform Scored 44 out of 56
[This message has been edited by Desai Sandeep (edited July 29, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
This is a bug 4155575. Supposedly it's been fixed in 'merlin-beta2' which I think is JDK 1.4.
You can check it at http://developer.java.sun.com/servlet/SessionServlet?url=http://developer.java.sun.com/developer/bugParade/index.jshtml
Just remember that for the purpose of the exam main must be public, static, void and take a String array for an argument.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Aakanksha
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Jane
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane!
-- Sandeep
 
Rashmi Hosalli
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane.It was interesting though to discuss this topic.
Thanks Aakanksha,Sandeep and e'body!
Rashmi.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But did you notice the fix?
"In 1.2Beta4 it is no longer required that the main method of an application be public."
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thomas,
Are you sure that's the fix? It didn't look like it. The report says
Reported Against 1.2beta4, 1.2.1, kestrel-beta, 1.2.2,
kestrel-rc2, kestrel-rc3, 1.0, 1.3, 1.1.7
Release Fixed merlin-beta2
So it looks like it was still a bug in 1.3. Which release is merlin-beta2? I thought it was 1.4?
reply
    Bookmark Topic Watch Topic
  • New Topic