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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

main method in base class

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Command Line: java Test
The above code compiles and prints "Hello".
How do you explain the behaviour of the above code?
Is it because even though main is not inherited by
class "Test", it is available to it and the JVM can
determine what to do?
Do you have a precise explanation?
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi rajsim,
all public members of base class are accessible & visible in the derived class( unless they are overridden by the derived class).
so in this case public static void main(string s[])
is available in the derived class. so the JVM executes it.
regards
deekasha
 
rajsim
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Deekasha,
Thanks for explaining. But the thing that is puzzling me is this:
change
public static void main
to
private static void main
It still works. I think this behaviour has something to do
with how the JVM executes the program.

[This message has been edited by rajsim (edited July 10, 2000).]
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
That is wired! I could not find anything in the Java specification to justify that. I believe that for the certification's meters this should not execute. I would suspect that the derived class has no "physical" access to the private methods of parent class, so the JVM or the compiler does something really interesting there.

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by rajsim:
Hi Deekasha,
Thanks for explaining. But the thing that is puzzling me is this:
change
[b]public
static void main
to
private static void main
It still works. I think this behaviour has something to do
with how the JVM executes the program.

[This message has been edited by rajsim (edited July 10, 2000).][/B]


This is very interesting!
It seems like the main method is just the entry point to the application. If you make it private in the base class, it is not accessible to the derived classes, but it is still available to the JVM. If you provide another main method in the derived class, public or private, that is the one that gets executed.
Example:
class abc{
int i = 017;
private static void main(String args[]){
System.out.println("Hello World!!" + i);
}
}
public class def extends abc{
int i = 20;
private static void main(String args[]){
System.out.println("Extended Hello World!!");
}
}
Here the main method from def is executed.
Savithri
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Any other explanations pals??
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi all

Just now i executed the program

For me the JVM cooly reports .......
Main method not public

mine is java version "1.4.2_07"

may be any version conflict........???
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
what i feel ..
when the classes are loaded if the JVM finds the method signature as
public static void main(String[] a) its executes it ..immaterial of where it is declared..coz


but if any of the method defination changes..i.e public to private


no output...

correct me if iam wrong ...
 
vidya sagar
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
hi srikanth

u had missed static keyword in main method in the second block of code

add static and check
 
Nila dhan
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

I executed the above code.The program compiled successfully but printed Main method not public in the console.My jdk version is 1.4.2_05.
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi..

Since 1.4...main method has to be public...


In the above program....the main method is treated as just another method..

and hence the program compiles...

But when you run the program...the environment...necessarily..and will look

for public static void main(String argument[])..

as the starting point..

which it doesnt find in this case...and so ...gives you the error..

Hope you got it..
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
"rajsim"

Please click on the My Profile link above and change your display name to match JavaRanch's Naming Policy of using your real first and real last names.

Thanks

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Wow, talk about waking the dead. This thread is 5 years old.

Please do not wake up old threads. Just create a new one, and if you have to you can link to the old one.

Closing this thread.

Mark
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
    Bookmark Topic Watch Topic
  • New Topic