• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Writing main Class

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sorry for asking but could any one help me out how to write a main method,

things are easy when i am writing the whole class code,

but if someone gives me a class and i have to write the main method for it,

how should i start and what things are to be primarily considered.

 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following is a simple java program which contains main method.


Furthermore, I would advice you to purchase any core java book such as "Head First Java" and start learning from it.


Regards
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Experienced programmer will help you in it..
But i think you should first understand what is the need of the program and then go for it by properly taking into consideration the behavior of all the classes.
 
Shailesh Phatak
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohd Abrar Khan wrote:
sorry for asking but could any one help me out how to write a main method,

things are easy when i am writing the whole class code,

but if someone gives me a class and i have to write the main method for it,

how should i start and what things are to be primarily considered.



Abrar are you a beginner??
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shailesh Phatak wrote:Abrar are you a beginner??


I'm wondering why did you ask that question??
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The contents of a main method depend *completely* on what you need it to do--there's no way to provide any generic advice.
 
Mohd Abrar Khan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies Guys,

I have apologized for asking this question at the beginning of this topic as i knew about the stupidity of this question,
ya i am a beginner,

the reason for me asking this question was that i came across a big class programme which had 6-7 methods and 3 subclasses and each doing a specific task and there was no main method for this class,

i tried writing the main method, but could'nt compile it properly so thought i might be missing something in calling all the functionalities of that class in my main method, so asked you guys for your suggestion,

ne ways was expecting some serious answers!!!

Abrar
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohd Abrar Khan wrote:I have apologized for asking this question at the beginning of this topic as i knew about the stupidity of this question,


Your question isn't stupid, so you don't need to apologize.

ne ways was expecting some serious answers!!!


As David said, what you do in the main method completely depends upon what you want to do. Generally the first step would be to create instances of the required class and calling any methods on them...
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a main method is just like any other method. if you want it to be the one that runs when you type "java MyClass", then it has to be "public static void main(String [] args)".

what it does is up to you. You could write a main() method that is completely unrelated to everything else in the file.

What do you WANT the main method to do, and what EXACT errors are you getting? if you post them, folks may be able to guide you.

Note that a common idiom for a main method is something like this:

 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohd Abrar Khan wrote:
ne ways was expecting some serious answers!!!

Abrar



Please use real words.

John.
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic