answersLogoWhite

0


Best Answer

That would require a detailed explanation of each of the keywords involved; if you are just starting to learn Java, suffice it to say that the Java Virtual Machine looks for a method that has that precise signature. As to what happens if you do otherwise, try it out! Probably you will get different error messages, depending on what keywords you omit or change.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why you need to declare main method as public static void main what happens if the method as private and non static?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you declared constructor as private?

If you declare the main method anything other than public, it will not run. If you declare main method as private, you would not be able to execute the class as a standalone java program. Any java class that needs to be executed as a standalone file needs to have a main method that is public, static and returns a void. Otherwise the Java compiler would not recognize the file as an executable standalone java file and would not allow you to run it.


Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }


What is meant by private visibilty of a method?

It means that the method is visible from only within the current method. Also, any class that wants to use or invoke the private method has to create an object of the class in which the method is created in order to access/invoke it. The private access modifier is the most restrictive of the four java access modifiers. The total opposite of private is public which gives access to everyone.


What happen if you omit public in java programming?

If you omit either the "public" or the "private" keywoard, the access (to a field or method) will be the so-called "default access". The field or method will be visible for other classes, if they are in the same package.


Can additional methods of base class be hidden from the derived class in object oriented software?

Yes. During inheritance only the public members of the parent class are visible to the child class. If you declare a method or a variable as private, the child class cannot access it. In other words, the methods and variables are hidden from the derived class.

Related questions

Private Finance Initiative?

is a method to provide financial support for 'public-private partnerships' (PPPs) between the public and private sectors.


Can you declared constructor as private?

If you declare the main method anything other than public, it will not run. If you declare main method as private, you would not be able to execute the class as a standalone java program. Any java class that needs to be executed as a standalone file needs to have a main method that is public, static and returns a void. Otherwise the Java compiler would not recognize the file as an executable standalone java file and would not allow you to run it.


Does java class must have public method?

no you can have a class with no public methods and even with a a private constructor public class Example { //constructor private Example(){ } }


What if you declare public members rather than private in c plus plus?

Public members in C++ have accessibility to any function that has scope to the instance of the class, whereas private members have accessibility only to functions of that class.


What is the major difference between public and private access method in computer programming?

public means this can be accessed from any class private means this can be accessed only from the current class


What is meant by private visibilty of a method?

It means that the method is visible from only within the current method. Also, any class that wants to use or invoke the private method has to create an object of the class in which the method is created in order to access/invoke it. The private access modifier is the most restrictive of the four java access modifiers. The total opposite of private is public which gives access to everyone.


Can a public bar hold a private party and refuse entry to the public?

Yes. It happens all the time. Think of a wedding reception, and a catering hall or a private restaurant.


Declare a class called rectangle which has two private members length and width and two public member functions called input and output?

class Rectangle{ private: int length; int width; public: void input(); void output(); };


Can a licensed public bar hold a private party and refuse entry to the public?

Yes. It happens all the time. Think of a wedding reception, and a catering hall or a private restaurant.


Can the void run method be declared as private?

Sure you can. The run method is just another java method that you can modify to be private or protected or have return types as Strings etc. But, this run method will not be invoked when you start the thread. Only the default public void run() will be invoked when you do a thread.start()


Can you declare main as final or not and why?

Yes you can. Try this: public class TestMain { /** * @param args */ public static final void main(String[] args) { System.out.println("Inside final mail method..."); } } It will print "Inside final mail method..." in the console.


How do you create an interface?

by declaring in the header of the class: public/private interface [interfaceName] and do not write any method body(i.e. public void dance();)