inner classes

Upload: sathish-narayanan

Post on 04-Mar-2016

212 views

Category:

Documents


0 download

DESCRIPTION

java inner classes

TRANSCRIPT

Slide 1

Inner classes are class within Class. Inner class instance has special relationship with Outer class.

This special relationship gives inner class access to member of outer class as if they are the part of outer class.

Inner class instance has access to all member of the outer class(Public, Private & Protected)123456//outer classclass OuterClass {//inner classclass InnerClass {}}Syntax for creating Inner Class

123456//outer classclass OuterClass {//inner classclass InnerClass {}}12outer.classinner$outer.classIf you compile above code it will produce two class file.

Note: You cant directly execute the inner classs .class file with java command.

class OuterClass {private int i = 9;// Creating instance of inner class and calling inner class functionpublic void createInner() {InnerClass i1 = new InnerClass();i1.getValue();}// inner class declarataionclass InnerClass {public void getValue() {// accessing private variable from outer classSystem.out.println("value of i -" + i);}}}WithinOuter ClassOuter class can create instance of the inner class in the same way as normal class member.How to access Inner ClassFrom Outside Outer Class

Create outer class instance and then inner class instance.class MainClass {public static void main(String[] args) {// Creating outer class instanceOuterClass outerclass = new OuterClass();// Creating inner class instanceOuterClass.InnerClass innerClass = new OuterClass.new InnerClass(); // Classing inner class methodinnerclass.getValue();}}OuterClass.InnerClass innerclass = outerclass.new InnerClass();javadocis a command-line tool for extracting special comments (calleddoc comments) from java source files and generating an easy-to-use HTML file tree containing them.

The comments can include standardHTMLmarkup and javadoc@tags. CommentDescription/* text */The compiler ignores everything from /* to */.// textThe compiler ignores everything from // to the end of the line./** documentation */This is a documentation comment and in general its calleddoc comment. TheJDK javadoctool usesdoc commentswhen preparing automatically generated documentation.The Java language supports three types of comments:A doc comment may be all on one line:

/** All on one line -- but still a doc comment. */ or span several lines:

/** Spans several lines still a doc comment */ The second and later lines of a multi-line doc comment

/** * For that 'retro' * FORTRAN look. */

may begin with whitespace and an asterisk, which javadoc discards.

Javadoc organization

The HTML output generated byjavadocis organized like java source is:

at the top level, as a group of several packages (with anoverview summary),

within a group, as individual packages (each with apackage summary),

within a package, as classes and interfaces (each with aclass or interface doc comment), and

within a class or interface, as fields (each with afield doc comment), and constructors and methods (each with aconstructor or method doc comment).

/** * The HelloWorld program implements an application that * simply displays "Hello World!" to the standard output. * * @author Zara Ali * @version 1.0 * @since 2014-03-31 */ public class HelloWorld { public static void main(String[] args) { /* Prints Hello, World! on standard output. System.out.println("Hello World!"); } } You can include required HTML tags inside the description part, for heading and has been used forcreatingparagraph break:/** * Hello, World! * The HelloWorld program implements an application that * simply displays "Hello World!" to the standard output. * * Giving proper comments in your program makes it more * user friendly and it is assumed as a high quality code. * * * @author Zara Ali * @version 1.0 * @since 2014-03-31 */ public class HelloWorld { public static void main(String[] args) { /* Prints Hello, World! on standard output. System.out.println("Hello World!"); } } The javadoc Tags: Thejavadoctool recognizes the following tags:TagDescriptionSyntax@authorAdds theauthorof a class.@author name-text{@code}Displays text in code font without interpreting the text as HTML markup or nested javadoc tags.{@code text}{@docRoot}Represents the relative path to the generated document's root directory from any generated page{@docRoot}@deprecatedAdds a comment indicating that this API should no longer be used.@deprecated deprecated-text@exceptionAdds aThrowssubheading to the generated documentation, with the class-name and description text.@exception class-name description{@inheritDoc}Inherits a comment from thenearestinheritable class or implementable interfaceInherits a comment from the immediate surperclass.{@link}Insertsan in-line link with visible text label that points to the documentation for the specified package, class or member name of a referenced class. T{@link package.class#member label}{@linkplain}Identical to {@link}, except the link's label isdisplayedin plain text than code font.{@linkplain package.class#member label}@paramAdds a parameter with the specified parameter-name followed by the specified description to the "Parameters" section.@param parameter-name description