answersLogoWhite

0


Best Answer

The static modifier means that it does not have to be instantiated to use it. Before a program runs there are technically no objects created yet, so the main method, which is the entry point for the application must be labeled static to tell the JVM that the method can be used without having first to create an instance of that class. Otherwise, it is the "Which came first, the chicken or the egg?" phenomenon. Your main method should be declared as follows: public static void main (String[] args) { lots of your java code... } As we know, java is a pure OOP , that means everything should be in the class, main. Aso, because main is itself a function, static member functions should not refer to objects of that class. But we can access static functions through classname itself, as: class TestMain { public static void main(String args[]) { body; } } Now, cmd>javac TestMain.java cmd>java TestMain as we know the static member functions has to call through its class name. That's why the programme name must be same as the class name ,where we wrote the main function. Another important point is : static variables or member functions will load during class. That means before creating any instances(objects), the main function is the first runnable function of any program which we run manually, such as : cmd>java TestMain(run) if , any sharing information.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

This is so you can run static methods, and can contain static variables.

Without it, this code would not work.

private static getint ()

{

return someint;

}

public void main(String[] args)

{

int i = getint();

}

It will return Cannot call static method in void context blah blah blah.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The main() method in any Java class always has the same signature, that is, public static void main(String args[]). The reason it is static is so that the JVM can call it directly without having to create an instance of the java class where it is declared. Any static java method can be called without having to instantiate the class that defines that static method. The reason we wouldn't want to force the JVM to create an instance of the java class to call the main() method is because creating this instance may be expensive and/or have unwanted side effects and/or require constructor arguments that the JVM doesn't know how to provide.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The java virtual machine first execute the static method and without create the object we cant execute .so using static we can execute .. and public is used to out side the classes we can access..this is the accesspecifire.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

Because, only a non static method can be invoked directly without creating an instance of the object and since the main is going to be the first method to be executed, it cannot be invoked first if it is non-static.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

becaused java are object orented programing languages its also used class .

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

static - so that the JVM can run the method without having to instantiate the class object

public - so that the JVM can access it freely without any access issues.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why should main be declared static and is declaring it public and void not sufficient?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

When you implement an interface method it should be declared as public in java?

This is not necessarily true. The only rules for this are that interface methods may not be private. They may be public, protected, or have the default (blank) access modifier.


Why user defined applet class declared public?

Well, it was probably an aesthetic decision on the part of the Java developers, and consider this: The server needs to access some of the applet's attributes, so doesn't it make sense that the applet is public? If the applet was private, nobody would be able to access it.


Which member should be declared as static?

Members which are shared among all instances of a class should be static.


Why member function of a class are generally declared as public and data members as private?

Declaring member variables (data) private ensure only the class and friends of the class have access to an object's data. Declaring member methods (functions) public is required to provide an interface to the private data. Usually accessor and mutator methods (get/set methods) are declared public to allow user-interaction with the data. However the data itself must remain hidden by the accessors (returned by value) otherwise there's no point in hiding the data -- there is no control. Similarly, the mutators should allow the data to be modified through a controlled interface, thus ensuring the data remains in a valid state at all times. Again, allowing a public mutator to modify the data without validation defeats the purpose of hiding the data in the first place. Protected access is another option. This is similar to private access, but also permits access to derived classes. A well-designed class interface should only expose as much as it needs to, and no more. If an object cannot assure its own data integrity at all times, then it is no better than a structure, which is always public by default. Note that the general rules on class interface design are intended to help not hinder you. Many new programmers find them too restrictive, often making members public when they should really be private. They have entirely missed the point of using a class rather than a structure. That is, a well-designed interface that fully encapsulates an object automatically enlists the help of the compiler to reduce the chances of you or a third party coder from attempting an illegal operation upon the data within the object. If the data integrity can be assured at all times, then the chance of error is greatly reduced. The more complex the data, the more important this becomes.


When should a member of a class be declared public?

When the member needs to be accessible from outside of the class. Private members are only accessible from within the class itself, including friends of the class. Protected members are the same as private members but are also accessible to derived classes. Derived classes therefore inherit both the protected and public members of its base classes.

Related questions

Do think that US was justified in declaring war on Germany?

At the start of the US involvement in WW II, the US was attacked by the Japanese. When the US declared that a state of war existed between the US and Japan, Germany declared war on the US. Your question should be was Germany justified in declaring war on the US.


Should an instance field be declared as public or private?

ask mrs. jain


What are the examples of Fiscal adequacy?

the source of revenue should be sufficient to meet the demands of public expenditure.. ---15---


What is fiscal adequacy in taxation?

this means that the source of revenue should be sufficient to address the demands of public expenditures.


The us should have waited to be attacked before declaring war during world war 2?

The US was attacked before declaring war in WW2, by the Japanese at Pearl Harbor. Does it make it right what Hitler was doing in Europe because the US declared war on them rather than vice versa ? Of course not.


a major newspaper runs an editorial declaring that the U.S. Senate should confirm a new Supreme Court Justice. What method of shaping public opinion does this represent?

Direct argument


When you implement an interface method it should be declared as public in java?

This is not necessarily true. The only rules for this are that interface methods may not be private. They may be public, protected, or have the default (blank) access modifier.


Is the centre declared for ssc board exam 2013?

Yes, the centers are declared. The list should be declared at the school.


Member modifier 'public' must precede member type and name means in computer C language?

While you declaring the global variable you should declare it correctly... This problem mostly arise because any one of the data type in global should not have variable.


What is a sentence with the term self-sufficient?

you should always be self sufficient and not depend on others only


How long should you wait in declaring bankruptcy after removing your name from a property deed?

It should be more than 30 days


Can you give me a sentence with the word sufficient?

The evidence provided was sufficient to support the conclusion.