Results 1 to 8 of 8

Thread: some queries

  1. #1
    Feng is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default some queries

    I got 3 questions regarding this:

    Java 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.

  2. #2
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    String args[] are the arguments that you pass to the program:
    Java Code:
    java Prog args args1 args2
    args are then the arguments, put in the args[] array like so
    Java 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
    Java Code:
    java Foo
    and it run. Otherwise you get an error.
    Last edited by staykovmarin; 11-24-2007 at 12:35 AM.

  3. #3
    Feng is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default

    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.

  4. #4
    Feng is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default

    any answer plz?

  5. #5
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Thats because you are required to read them. The Java compiler recognizes that method to be the main method, and starts execution from there.
    Java 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.

  6. #6
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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

  7. #7
    Feng is offline Member
    Join Date
    Nov 2007
    Posts
    13
    Rep Power
    0

    Default

    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)
    Java 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 + ".");
    	}
    }

  8. #8
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    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:
    Java Code:
    AverageTwoDoubles avg = new AverageTwoDoubles();
    avg.start()
    That will run the code inside the run() method.


    If ConsoleProgram implements Runnable
    Java 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.

Similar Threads

  1. help me need to understand queries
    By hossainsadd in forum JDBC
    Replies: 1
    Last Post: 05-26-2008, 01:02 AM
  2. Replies: 0
    Last Post: 04-15-2008, 02:32 AM
  3. How to run Queries in Spring framework
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 04-01-2008, 11:42 AM
  4. queries regarding SWT
    By ravian in forum New To Java
    Replies: 0
    Last Post: 11-26-2007, 04:06 PM
  5. How to run Queries in Spring framework
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-28-2007, 02:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •