Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Lab 03: Java Virtual machine (JVM) and Java Documentation Objectives: To : gain experience with class initialization and object construction. get hands on and practice with javadoc utility. 1. Class initialization and Object construction In Java code, a proper initial value is specified via a class variable initializer or static initializer. A class variable initializer is an equals sign and expression next to a class variable declaration, as in: class Example1a { // "= 3 * (int) (Math.random() * 5.0)" is the class variable initializer static int size = 3 * (int) (Math.random() * 5.0); } A static initializer is a block of code introduced by the static keyword, as in: class Example1b { static int size; // This is the static initializer static { size = 3 * (int) (Math.random() * 5.0); } } All the class variable initializers and static initializers of a type are collected by the Java compiler and placed into one special method. For classes, this method is called the class initialization method; for interfaces, the interface initialization method. In Java class files for both classes and interfaces, this method is named "". Regular methods of a Java application cannot invoke a method. This kind of method can only be invoked by the Java virtual machine, which invokes it to set a type's static variables to their proper initial values. Initialization of a class consists of two steps: Initializing the class's direct superclass (if any), if the direct superclass hasn't already been initialized. Executing the class's class initialization method, if it has one. When initializing a class's direct superclass, the same two steps listed above must be followed. As a result, the first class that will be initialized will always be Object, then all the classes on down the inheritance hierarchy to the class being actively used. Superclasses will be initialized before subclasses. The above imply that, the first class that gets initialized is Object. •Note that static final variables are not treated as class variables but as constants and are assigned their values at compilation. •After a class is loaded, linked, and initialized, it is ready for use. Its static fields and static methods can be used and it can be instantiated. •When a new class instance is created, memory is allocated for all its instance variables in the heap. •Memory is also allocated recursively for all the instance variables declared in its super class and all classes up is inheritance hierarchy. •All instance variables in the new object and those of its superclasses are then initialized to their default values. •The constructor invoked in the instantiation is then processed according to the following rules : •Rules for processing a constructor The constructor invoked in the instantiation is then processed according to the following rules :: 1.Assign the arguments for the constructor to its parameter variables. 2.If this constructor begins with an explicit invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively. 3.If this constructor is for a class other than Object, then it will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively. 4.Initialize the instance variables for this class with their proper values. 5.Execute the rest of the body of this constructor. Finally, the reference to the newly created object is returned as the result. Example 1: class SuperclassA { private int vara=initializerExpression(); //1a private static int i=staticinitializer(); //1b static //1c { System.out.println("static initializer block in superclassA"); } { //2 System.out.println("instance initializer block in superclassA + vara= "+vara); } public SuperclassA( ) { //3 System.out.println("Constructor in SuperclassA"); } private int initializerExpression() { //4 System.out.println("Instance initializer expression in SuperclassA"); return 5; } public static int staticinitializer() { // 5 System.out.println("static initializer expression in SuperclassA"); return 5; } } class SubclassB extends SuperclassA { public SubclassB() { // 6 this(3); System.out.println("Default constructor in SubclassB"); } public SubclassB(int i) { //7 super(); System.out.println("Non-default constructor in SubclassB"); value = i; } private int value = initializerExpression(); // 8 static //9 { System.out.println(" static initializer block in subclassB"); } { //10 System.out.println("Instance initializer block in SubclassB"); value = 2; } private int initializerExpression() { //11 System.out.println("Instance initializer expression in SubclassB"); return 1; } } public class ObjectConstruction { public static void main(String args[]) { new SubclassB(); //12 // System.out.println("\n\n"); //13 // new SubclassB(); //13 } } 2. Literate Programming Java has a standard form for inserting documentation comments that describe classes, methods and fields inside a source file.Java SDK tool, called javadoc can then be used to automatically generate a neat set of HTML pages that document the classes. Documentation comments are delimited by: ………… */ The javadoc utility copies the first sentence of each comment to a summary table. Therefore, it is best to write that first sentence with some care. It should start with an uppercase letter and end with a period. It does not have to be a grammatically complete sentence, but it should be meaningful when it is pulled out of the comment and displayed in a summary. /** Within a documentation comment, we can use the following tags to highlight some information in the generated HTML document. Tag @author @deprecated @exception @param @return @see @since @throws @version Meaning Identifies the author of the class Specifies that a class member is deprecated Identifies an exception thrown by a method Documents a method's parameter Documents a method's return value Specifies a link to another topic States the release when a specific change was introduced Same as the exception tag Specifies the version of a class Method comments should contain additional information for each method parameter using the @param tag. You should also use the @return to indicate the return type of the method. You omit the @param tag for methods that have no parameters (parameterless), and you omit the @return tag for methods whose return type is void. usage: javadoc [options] [packagenames] [sourcefiles] [classnames] [@files] -public Show only public classes and members -protected Show protected/public classes and members (default) -package Show package/protected/public classes and members -private Show all classes and members -help Display command line options -version Include @version paragraphs -author Include @author paragraphs Example 2: The following example shows the class Greeter using literate programming to generate meaningful documentation with javadoc utility. /** A class for producing simple greetings. */ public class Greeter { public String name; /** Constructs a Greeter object that can greet a person or entity. @param aName the name of the person or entity who should be addressed in the greetings. */ public Greeter(String aName) { name = aName; } /** Greet with a "Hello" message. @return a message containing "Hello" and the name of the greeted person or entity. */ public String sayHello() { return "Hello, " + name + "!"; } } First, the javadoc utility will format your comments into a neat set of HTML documents. It makes good use of the seemingly repetitive phrases. The first sentence of each method comment is used for a summary table of all methods of your class (see Figure 1). The @param and @return comments are neatly formatted in the detail description of each method (see Figure 2). If you omit any of the comments, then javadoc generates documents that look strangely empty. D:\WORKAREA> javadoc Greeter.java 3. Assignments 1. Identify the different parts of Example 1 and explain the output. Compile and run the program. Which class of the above example is executed first?…………..justify your answer ……………………………………………………….. 2- then remove the comment part found in 13 and predict the output of the program // System.out.println("\n\n"); //13 // new SubclassB(); //13 Explain the difference between the previous output and this one. 3. Practice by accessing java 2 SDK documentation. Look for index.html ----- API & Language Documentation ---- Java 2 Platform API Specification Read about random method( ) found in java.lang package Class java.lang.Object | +--java.lang.Math Math 4.. generate the necessary documentation for the classes designed in Lab 2. Note if you want to include @version or @author in your program you must do the following Javadoc Test.java –version -author