Computer Programming
Java Programming
C Sharp

Why should main be declared static and is declaring it public and void not sufficient?

666768

Top Answer
User Avatar
Wiki User
2009-04-10 19:44:01
2009-04-10 19:44:01

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.

123
๐Ÿ™
0
๐Ÿคจ
0
๐Ÿ˜ฎ
0
๐Ÿ˜‚
0

Related Questions

User Avatar

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.

User Avatar

Applet class should be declared public so that it can be accessed outside when it is run on browser or thro' appletviewer tool. -Shini Saji

User Avatar

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

User Avatar

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

User Avatar

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.

User Avatar

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

User Avatar

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.

User Avatar

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.

User Avatar

As a result of the 4 resolutions from congress (asked by McKinley), Spain declared war on the U.S the three resolutions included declaring that Cuba should be independent, demanding Spain to back off Cuba, allowing the president to use force to get rid of the Spanish, the last didn't amount to war but settled the issue if Cuba should be annexed.

User Avatar

The content of this sentence should be sufficient to satisfy your needs.

User Avatar

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

User Avatar

The International Astronomical Union declared in 2006 that Pluto should be reclassified as a dwarf planet.

User Avatar

Yes, they should have sufficient homework. The exact quantity comprising "sufficient" is, potentially, debatable.

User Avatar

A bit such as a kimberwhick or hackmore should be sufficient.

User Avatar

This question lacks sufficient detail to answer...

User Avatar

Because I am self sufficient.

User Avatar

Yes, but since it is public there is no need to; any code outwith the class can access a public data member directly. If it were a private data member, then returning by reference would defeat the point of making it a private data member in the first place; you might as well make it public. Private data members should always be returned by value, never by reference and never by pointer, and data members should only be declared public if they are not critical to the internal operation of the class.

User Avatar

The center name should be declared by the school . They are generally declared before the practicals begin.

User Avatar

No emperor declared that the empire should be split into two parts. It was historians that made the distinction in order to avoid confusion.No emperor declared that the empire should be split into two parts. It was historians that made the distinction in order to avoid confusion.No emperor declared that the empire should be split into two parts. It was historians that made the distinction in order to avoid confusion.No emperor declared that the empire should be split into two parts. It was historians that made the distinction in order to avoid confusion.No emperor declared that the empire should be split into two parts. It was historians that made the distinction in order to avoid confusion.No emperor declared that the empire should be split into two parts. It was historians that made the distinction in order to avoid confusion.No emperor declared that the empire should be split into two parts. It was historians that made the distinction in order to avoid confusion.No emperor declared that the empire should be split into two parts. It was historians that made the distinction in order to avoid confusion.No emperor declared that the empire should be split into two parts. It was historians that made the distinction in order to avoid confusion.

User Avatar

when alarm yellow is declared.

User Avatar

87 octane should be sufficient

User Avatar

It should provide sufficient cooling as long as your computer is cool enough as well.

User Avatar

An icepack should be sufficient. Anything more you should seek medical advice.


Copyright ยฉ 2020 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.