Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-23-2007, 11:45 PM
Member
 
Join Date: Nov 2007
Posts: 13
Feng is on a distinguished road
some queries
I got 3 questions regarding this:

Code:
public static void main (String [] args) {
a) there are many times that in order to run a program, we have to type String [] args. Why we use this? And why some times we dont need to type it? What does it mean?

b)why in some programs we type run instead of main?

c)when do we type static? only at the main method of our main class?

Last edited by Feng : 11-24-2007 at 12:44 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-24-2007, 12:21 AM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
String args[] are the arguments that you pass to the program:
Code:
java Prog args args1 args2
args are then the arguments, put in the args[] array like so
Code:
args[0] = args args[1] = args1 args[2] = args2
run() is only used in threads, if i am understanding you right.

static has a meaning too long to explain in a single post. You might want to read the java tutorials or get yourself a good book.

edit: you always need to have a main method. It is the entry point to your program. The class that has your main method is the class that you do
Code:
java Foo
and it run. Otherwise you get an error.

Last edited by staykovmarin : 11-24-2007 at 12:35 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-24-2007, 12:49 AM
Member
 
Join Date: Nov 2007
Posts: 13
Feng is on a distinguished road
i dont put any args, at least up to now. but i have many times to write this String [] args in order not to have problems.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-24-2007, 12:25 PM
Member
 
Join Date: Nov 2007
Posts: 13
Feng is on a distinguished road
any answer plz?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-24-2007, 07:37 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Thats because you are required to read them. The Java compiler recognizes that method to be the main method, and starts execution from there.
Code:
public static void main(String arg)
that method will not be recognized by the compiler, and you will get an error if no other main method exists.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-24-2007, 07:46 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
a — See the main method
b — run instead of main I don't know what you're talking about. Do you have an example of this?
c — when do we type static See Understanding Instance and Class Members
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-24-2007, 08:22 PM
Member
 
Join Date: Nov 2007
Posts: 13
Feng is on a distinguished road
b — run instead of main I don't know what you're talking about. Do you have an example of this?

an examples (with the use of an external library though)
Code:
import acm.program.*; public class AverageTwoDoubles extends ConsoleProgram { public void run() { println("This program averages two numbers."); double n1 = readDouble("Enter no1: "); double n2 = readDouble("Enter no2: "); double average = (n1 + n2) / 2; println("The average is " + average + "."); } }
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-24-2007, 09:10 PM
Senior Member
 
Join Date: Nov 2007
Location: Newport, WA
Posts: 141
staykovmarin is on a distinguished road
Is ConsoleProgram a subclass of Thread, or is it implementing Runnable? If it was, then what you are doing when you create a run() method, is you are telling it what the thread will execute (by overriding the run() method)

If ConsoleProgram extends Thread:
Code:
AverageTwoDoubles avg = new AverageTwoDoubles(); avg.start()
That will run the code inside the run() method.


If ConsoleProgram implements Runnable
Code:
Thread t = new Thread(new AverageTwoDoubles()); t.start();
That will run the code in the run() method (again in a separate thread)

edit: if ConsoleProgram doesn't extend Thread, nor implement Runnable, then you are simply creating a method called run() that executes some code.

Last edited by staykovmarin : 11-24-2007 at 09:14 PM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
help me need to understand queries hossainsadd Database 1 05-26-2008 01:02 AM
pls somebody help me ; need to understand SQL queries from DAOImpl class hossainsadd Web Frameworks 0 04-15-2008 02:32 AM
How to run Queries in Spring framework Java Tip Java Tips 0 04-01-2008 11:42 AM
queries regarding SWT ravian New To Java 0 11-26-2007 04:06 PM
How to run Queries in Spring framework JavaBean Java Tips 0 09-28-2007 02:00 PM


All times are GMT +3. The time now is 08:06 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org