Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

main() is the entry point

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am a bit confused here.

main() is the entry point for all applications. main() is the static method. So as is expected a static method can call from within another static method or static variable.

So my question is

methods called from within the main() method must be static. Variables called must be static.

So does it not mean that everything will have to be static ?
 
Dave Salter
Ranch Hand
Posts: 293
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Methods called from within main() do not have to be static if they belong to a class that you have instantiated within main().

So, you can instantiate classes within main() and call methods on them.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12085
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
often, one of the first things you'll see in Class A's main method is a creation of an object of type A... i.e. within main you'll see

A myA = new A();

now you can call A's non-static methods within main, on a

myA.nonStaticMethod1();
myA.nonstaticMethod2();

etc.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think a simple example here is worth 1000 words.

 
Arnb Sen
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ok.. Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic