File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Writing main Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Writing main Class" Watch "Writing main Class" New topic
Author

Writing main Class

Mohd Abrar Khan
Greenhorn

Joined: Jan 20, 2010
Posts: 18

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.

Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

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


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
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.


See Dreams With Open Eyes And Make It Real
Shailesh Phatak
Ranch Hand

Joined: Aug 27, 2009
Posts: 64
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??
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Shailesh Phatak wrote:Abrar are you a beginner??

I'm wondering why did you ask that question??


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Jan 20, 2010
Posts: 18
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

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

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:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Mohd Abrar Khan wrote:
ne ways was expecting some serious answers!!!

Abrar


Please use real words.

John.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing main Class
 
Similar Threads
OO Calculator -- a Tutorial
Linked List equals method
Guide me Plz
Reuse database connection class
Why take code out of Main?