• Post Reply Bookmark Topic Watch Topic
  • New Topic

main() method in java  RSS feed

 
suma bn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why main() method should be declared inside the class? As in c++, why can't we define outside the class?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

In Java, methods are always defined inside a class. Java does not have stand-alone methods. That's just how the Java programming language works. The main() method is just a method, like any other method, so it has to be defined inside a class.

I think that in C++ the only reason it is possible to have global functions (outside of a class) is because the designers of the C++ language wanted it to be backwards compatible with C. Java is not C++, nor is it designed to be compatible with C++ or C, so there's no reason to think why in Java it should work in the same way as in C++.
 
Jam Rei
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those answers from Yahoo! were not nearly as good as you get here
 
Jam Rei
Greenhorn
Posts: 29
IntelliJ IDE Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, it was the first and last time. I promise!
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are happy for you to look anywhere, but we often provide better answers than the other places
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!