go to previous page   go to home page   go to next page

Answer:

Yes. It is OK to have a return statement at the end of a method, even though it is not needed. In this example the return does not return a value to the caller, which agrees with the void return value for the method.


Driver Class

The Java interpreter starts a program by looking for a static main() method inside of the HelloTester.class file. Since the method is static the interpreter can run it without first constructing an object.

It is convenient to have a separate class that serves no other purpose than to contain the main() method. This testing class is used to start things running. Sometimes the class that contains main() is called a driver.

Usually main() constructs objects of various classes and calls their methods. These objects do the real work of the program.

The source file for the program is named HelloTester.java. When you compile the file, the compiler outputs two separate files of bytecodes, one for each class:


C:\JavaCode>javac HelloTester.java

C:\JavaCode>dir

11/13/2017  10:07p                   257 HelloTester.java
11/13/2017  10:40p                   476 HelloObject.class
11/13/2017  10:40p                   373 HelloTester.class
               3 File(s)          1,106 bytes
               
C:\JavaCode>java HelloTester
Hello from an object!
C:\JavaCode>               

Each class defined in the source files compiles into a file of bytecode. To run the program, type:

java HelloTester

The Java interpreter finds the main() method in the HelloTester class and starts it running.

Of course, with an IDE like BlueJ you run the program directly from the visual interface. Class files are still created, however, and can be found somewhere in the directories created for the project. The bytecode files created with an IDE are exactly the same as those created from the command line. The compiler is the same. The IDE just provides a convenient way to organize a project.


QUESTION 10:

When the Java interpreter needs the definition HelloObject, where will it be found?


go to previous page   go to home page   go to next page