Skip to main content

create a test class (main method) to start(run) the class in Java

3 replies [Last post]
curious725
Offline
Joined: 2012-07-31
Points: 0

Maybe I do not understand something.
So I've created the class in Java, which does some stuff.I need to create a main method or mainClass, that will start(run) my class. (I do not mean JUnit).
Question: how to do it?
is there some sort of methodology?
described in the books somewhere?

Help me please

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
skydan
Offline
Joined: 2012-08-01
Points: 0

You are missing very basic knowledge about Java. You should definitely pick a book (like 'Big Java') and read it.

To answer your question:
You have to define a method that looks like public static void main(String[] args). This method will be run by the JVM when you launch your class: java MyClassName.class

skydan
Offline
Joined: 2012-08-01
Points: 0

.

mohana_sundaram
Offline
Joined: 2012-08-02
Points: 0

We have two methods to use the thread
1).To extend the Thread class.
Example:

public class test extends Thread {
   
    /**
     * This method is executed when the start() method is called on the thread
     * so here you will put the 'thread code'.
     */
    public void run() {
        System.out.println("My Thread executed Successfully!");
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Thread thread = new test();
        thread.start();
    }
}

2).To implement Runnable Interface

Example:

public class test implements Runnable {
   
    /**
     * As in the previous example this method is executed
     * when the start() method is called on the thread
     * so here you will put the 'thread code'.
     */
    public void run() {
        System.out.println("Thread executed!");
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

   //create a Thread object and pass it an object of type Runnable
        Thread thread = new Thread(new test());
        thread.start();
    }
}