main method in base class
posted 25 years ago
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?
-
-
Number of slices to send:Optional 'thank-you' note:
-
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?
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
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
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
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
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).]
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).]
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
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.
posted 25 years ago
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
-
-
Number of slices to send:Optional 'thank-you' note:
-
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
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
Any other explanations pals??
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
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........???
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........???
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
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 ...
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 ...
Thanks & Regards<br /> <br />-Srikanth
vidya sagar
Ranch Hand
Posts: 580
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
hi srikanth
u had missed static keyword in main method in the second block of code
add static and check
u had missed static keyword in main method in the second block of code
add static and check
Nila dhan
Ranch Hand
Posts: 161
posted 20 years ago
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.
-
-
Number of slices to send:Optional 'thank-you' note:
-
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.
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
Hi..
Since 1.4...main method has to be coderanch...
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..

Since 1.4...main method has to be coderanch...
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..

posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
"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
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
posted 20 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
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
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
| All of life is a contant education - Eleanor Roosevelt. Tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |











