What happens when a main method is declared as private?

Answers were Sorted based on User's Feedback



What happens when a main method is declared as private?..

Answer / dharma

main method signature must be like

public static void main(String args[])

if we declare as private then it compiles successfully

but at Runtime it says

"Main method not found in class"

Is This Answer Correct ?    1 Yes 0 No

What happens when a main method is declared as private?..

Answer / chinmay

If main method declared as private then at compilation no problem but at run time it shows an error that main method should be public...
because we access the main() method out side from class if it is private then it cannot be accessible...
that's why we have to write public in main method signature...

Is This Answer Correct ?    1 Yes 0 No

What happens when a main method is declared as private?..

Answer / maduakor ngozi chukwukodinaka

When a method is declared as private, the program compiles
properly but it will give runtime error ?Main method not
public".

Is This Answer Correct ?    0 Yes 0 No

What happens when a main method is declared as private?..

Answer / pankaj kumar

I have checked it proper in C# using visual studio.
its giving right output without any error at compile time or run time.

Is This Answer Correct ?    0 Yes 0 No

What happens when a main method is declared as private?..

Answer / pankaj kumar

i have checked it using c# in visual Studio.
then i found that there is no error in compilation or in run.
it is giving right output as we want without any error.

Is This Answer Correct ?    0 Yes 0 No

What happens when a main method is declared as private?..

Answer / milanjeet singh

When we define main method as private, it will compile but when run, it show an Error as: main method is not found in the class, please define the main method as public static void main (String[] args)

Is This Answer Correct ?    0 Yes 0 No

What happens when a main method is declared as private?..

Answer / sachin

JVM wants to execute the class its starting Execution from main Method means if JVM wants to execute main method if we declare main Method as private then its restriction the scope of main method if we declare Main Method as public then only jvm can applicable to access the main Method.
for e.g
class SE3
{
private static void main(String[] args)
{
System.out.prinln("Hello");
}
}
E:A_coreResearch_Programs>javac SE3.java

E:A_coreResearch_Programs>java SE3
Error: Main method not found in class SE3, please define the main method as
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

Is This Answer Correct ?    0 Yes 0 No

What happens when a main method is declared as private?..

Answer / thiru

The default signature should not be altered. If any changes, at run time "Class xxx does not have main method" exception will be thrown. But compilation will not give any exception.

Is This Answer Correct ?    0 Yes 0 No

What happens when a main method is declared as private?..

Answer / pawan

vani, varsha is correct , the 'private main method' is as
good as any member method your class can have (note that
main is not a key word in java) , it can be only used inside
the class in which it is defined.

It is clearly mentioned in the specifications that the
executing class should contain a 'public static void method'
as a point of execution otherwise execution will fail
complaining ABOUT THE METHOD WHICH I MENTIONED ABOVE.

Is This Answer Correct ?    2 Yes 3 No

What happens when a main method is declared as private?..

Answer / r.jainrocks@gmail.com

in above answer first word is ByteCode

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More Core Java Interview Questions

What is the difference between instanceof and isinstance?

0 Answers  


I Have a class abstract with one abstract method, so that method should override in the subclass, but i dont want to override, if i am not override what will happen? If compilation will occur then i dont want to give compilation error, then what we need to do??? See the sample program. public abstract class AbstractExample { public abstract void sampleMethod(); } public class AbstractExampleImple extends AbstractExample { }

2 Answers   Mphasis,


Does every java program need a main?

0 Answers  


what is deadlock in threads? with example

2 Answers   CTS, XOXO,


Can we increase size of array?

0 Answers  






what is anonymous class in java?

0 Answers  


How do u provide security in java

3 Answers  


Why is sizeof not a function?

0 Answers  


Explain the features of interfaces in java?

0 Answers  


How do you differentiate abstract class from interface?

0 Answers  


What is difference overloading and overriding?

0 Answers   Cyient,


Can we declare the static variables and methods in an abstract class?

0 Answers  


Categories