CS 310, Object-Oriented Programming with Java

Quiz 12, 17 April 2008


  1. How does Java code written to be both an application and an applet know which it is when run?


    It knows by the first method called when run. An application starts with a call to the main() method, while an applet starts with a call to the init() method. (It's an Applet. It's an Application. It's both!, page 520 (7th ed.) or 532 (8th ed.))

    The answers, in no particular order and approximately verbatim:

    • An applet is the same thing as an application, however it is necessary to make it an HTML file so that it can run in the browser and the main needs to be taken out.

    • Depending on the environment it is being run in, the Java code should test to see in what context it is being called.

    • Java code knows the difference between an app and an applet because an applet does not contain a main function. An applet also uses the “init” method to show that the following code is an applet.

    • An applet is started with init(). An application is started with a main().

    • Code written to be both an application and an applet know which is which because the class extends applet. Applets are run using the virtual machine. The virtual machine knows to build the code appropriately.

    • The applet code will contain a method call init instead of main, no true exit parameters, and html “applet” tags (or “object” tags) specifying it is an applet and where to load it from, along with dimensions and location on the webpage.

    • The application uses the constructors to create objects and applets use the init() function to create objects.

    • If it is run through <applet> tags in an html file, then it is an applet. If it is run through a wrapper program that runs it from the OS, it is an application.

    • The “applet” will only run when called by another program. If just the “application” is run, it will run. The difference between the 2 comes from how the program is run.

    • It is determined whether or not it is being called as an applet or an application. Ex. calling an applet from Javascript.

    • The presence of a frame as opposed to a browser window.

  2. Class files and associated resource files can be linked together by putting them in the same package. However, what mechanism actually allows a class file to retrieve its associated resources?


    The trick is recognizing that resource files are not class files and can't be handled in the way that class files are, particularly with respect to location and the class path. It's true that resource and class files can be combined in the same jar file, but they can also be placed in separate jar files. Something more than jar files is required.

    The class loader is the missing piece. It remembers the packages from which it loads classes and assumes that a resource requested by a class is found in the same package as the class. This is why class instances loading resources have to use the getResource() method associated with the Class class. Page 529 (7th ed.) or 498 (8th ed.).

    The answers, in no particular order and approximately verbatim:

    • Classes can retrieve associated resources through the classpath. The classpath is either set through an IDE or can be set as a flag when using javac.

    • By putting resource files of a class in the same package as the class file, a “nickname” is created so that resource can be used without specifying its exact location. Instead of this the entire path to the resource file can be used instead of the automatic nickname generated.

    • It uses method calls to retrieve resources from the package.

    • The manifest file points the classes to where the resources are located — even to absolute URLs.

    • import anyprog.*; will bring this resource into the class file. This also requires the resources to be in the class path.

    • The sealing mechanism makes this possible by allowing classes and resources in one package to access each other, and not allowing outside jars to access the classes and resources.

    • Files have implied access to the package that they are in, which prevents the need for an import statement.

    • In order to linked together class files and their resources it is necessary to create a manifest.

    • The class files and associated resource files that are put into the same package will be loaded into a .jar file. This will ensure the files inside the jar can be accessed but can not have the source modified. The mechanism that allows the class file to retrieve class files is that an alias of all resource files is created inside the package that allows class files to retrieve associated resources.

    • A stream object specific to the resource (audio stream, file stream).


This page last modified on 31 March 2008.