• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Why does a non-static main method throw an error

 
Ranch Hand
Posts: 118
Android Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
was trying to understand the signature of main method.

public static void main(String[] args)
{

}

the above code compiles without an error.

now when the change i.e. make the main method non-static

public void main(String[] args)
{

}

the above code throws an error..

now i understand that static keyword binds the method to the class and the static method needs to be accessed in a static way i.e. with the help of class name

i.e for example zoo.main();

Y does a non-static main method throw error and y cannot main method be non-static...
 
Sheriff
Posts: 17316
299
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be more specific. What is the exact error message? Is it a compile-time error or run-time error?
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Objective C Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI

its a runtime error...

Error: could not find or load main class static..
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because java command looks for a method with signature public static void main(String[] args).
If it can't find one it prints the error message.
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The creators of Java decided that that was the signature that was needed to start a Java program. They could have decided to allow non-static main methods if they wanted, but they didn't.

We can only speculate why that is, but it's probably because they were trying to make it as similar to the entry point of a C/C++ program as possible (to keep it familiar). These languages have a global method called main(). Static methods are the closest you get to global methods in Java.
 
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic