View RSS Feed

Tutorial

Object-oriented programming tutorials in Java.

  1. Annotation Processing

    by , 04-26-2012 at 06:27 PM
    Uses of advanced annotation comprises of the annotation processor writing that reads the Java program. Actions are taken based at the annotations. Generation of the auxiliary source code takes place that would relieve programmer to create the boilerplate code,that will be following the predictable patterns. 5 of JDK will be released to facilitate this task, which will be including annotation processing tool. When 6 of JDK is released, functionality of apt is the standard part of java compiler. ...
    Categories
    Tutorial
  2. Annotations

    by , 04-26-2012 at 06:26 PM
    Annotations provide that data regarding program which is not program’s part. No direct effect they have at the code operation, they annotate.

    Uses of the annotations are as following:

    • Information for compiler: Annotations can be used by the compiler for detection of the errors or suppress warning.
    • Deployment and compiler-time processing: Software tools can process the annotation information so that to generate the code, XML files or so forth.
    • Runtime processing:
    ...
    Tags: annotations Add / Edit Tags
    Categories
    Tutorial
  3. Enum Types

    by , 04-26-2012 at 06:24 PM
    An enum type is considered to be a type which possesses fixed constants set in its fields. Few common examples are the week days and compass directions (values of EAST, WEST, NORTH & SOUTH).
    Enum type fields comprises of uppercase letters, as they are constants.

    Enum keywords define the enum type, in Java programming language. For example, enum type of week days shall be specified like this:
    Java Code:
    public enum Day {
        SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    ...
    Tags: enum types Add / Edit Tags
    Categories
    Tutorial
  4. Passing Reference Data Type Arguments

    by , 04-26-2012 at 06:23 PM
    Reference data type parameters also get passed by the value into methods. It shows that by returning methods, same objects are referred by the passed in reference. But, the object’s field value might get changed in case there is a proper access level.

    Let us take a method into consideration present in arbitrary class, which will be moving the Circle objects.
    Java Code:
    public void moveCircle(Circle circle, int deltaX, int deltaY) {
        // code to move origin of
    ...
  5. Passing Primitive Data Type Arguments

    by , 04-26-2012 at 06:21 PM
    Primitive arguments for example, a double or an int, get passed by value into methods. This shows that changes to parameter values are present just in the method’s scope. When methods are returned it means that parameters have been gone & all alterations have been lost. For example:
    Java Code:
    public class PassPrimitiveByValue {
    
        public static void main(String[] args) {
               
            int x = 3;
               
            // invoke passMethod()
    ...
  6. Parameter Names

    by , 04-26-2012 at 06:18 PM
    When a parameter is declard to a constructor or a method, it means that a name is provided for that parameter. Within method body, this name is used so that for referring to the argument that has passed-in.
    Parameter’s name shall be unique, in its scope. It is not possible to be similar to the other parameter’s name for the same constructor or method, or the local variable’s name in constructor or method.

    Parameter may be having similar name just like that of class’s field. ...
    Categories
    Tutorial
  7. Arbitrary Number of Arguments

    by , 04-26-2012 at 06:17 PM
    A construct known as varargs could be used so that for passage of the arbitrary values number to a method. Varargs can be used when numbers of argument types that shall be passed are not known. This is actually the shorter way for array creation that is done manually.

    For usage of the varargs last parameter type shall be followed (by an ellipsis). Then shall come space, then parameter name. Call method by any number, of that parameter, which will also include none
    Java Code:
    public
    ...
  8. Parameter Types

    by , 04-26-2012 at 06:16 PM
    Any data type could be used for the constructor’s or method’s parameter. Primitive data types are included in it like floats, integers and doubles, as it has already been seen in computePayment method. Reference data types are also included like arrays or objects.

    Method which will be accepting array to be as argument has been shown in the given example. In this given example a new polygon object is being created by the method which initialized from Point objects array.
    ...
    Categories
    Tutorial
  9. Passing Information to a Method or a Constructor

    by , 04-26-2012 at 06:15 PM
    Declaration for constructor or a method makes declared the type & number of the arguments for that constructor or method. For instance, given method computes the home loan monthly payments which depend upon the loan amount, loan length, interest rate or the loan’s future value.
    Java Code:
    public double computePayment(
                      double loanAmt,
                      double rate,
                      double futureValue,
                      int numPeriods) {
    ...
    Categories
    Tutorial
  10. Naming a Method

    by , 04-26-2012 at 06:14 PM
    Though a method name could be code conventions or legal identifier restrict method names. Method names shall be multi worded name which will be starting with verb, in lowercase preceeded by nouns, adjectives etc or they shall be verb, in lowercase. In case of multi word names, it is necessary that 1st letter of every second & preceeding words shall be made capital. Few examples are as following:

    run
    runFast
    getBackground
    getFinalData
    compareTo
    ...
    Categories
    Tutorial
  11. Defining Methods

    by , 04-26-2012 at 06:13 PM
    Typical method declaration is being explained by this example:
    Java Code:
    public double calculateAnswer(double wingSpan, int numberOfEngines,
                                  double length, double grossTons) {
        //do the calculation here
    }
    Method return name, type, body b/w braces & a parentheses () pair are the required method declaration elements.

    There are 6 components of the method declaration, in general.

    1. Modifiers
    ...
    Categories
    Tutorial
  12. Declaring Member Variables

    by , 04-26-2012 at 06:12 PM
    Various kinds of variables are present
    • Member variables which are present in a class are known as fields.
    • Variables in block of code or method are known as local variables.
    • Variables in method declarations—these are called parameters.



    These code lines are used by the Bicycle class so that for defining its fields:
    Java Code:
    public int cadence;
    public int gear;
    public int speed;
    There are 3 components of field declaration which ...
    Categories
    Tutorial
  13. Declaring Classes

    by , 04-26-2012 at 06:10 PM
    Classes have been seen that are defined in this way:
    Java Code:
    class MyClass {
        // field, constructor, and 
        // method declarations
    }
    This is what is known as the class declaration. All codes are present in class body which gives out for the objects that have been created by the class. Declaration is for sake of the fields that give the state of objects and class & also the methods for implementation of the behavior of objects and class. Constructors ...
    Categories
    Tutorial
  14. Calling an Object's Methods

    by , 04-26-2012 at 06:09 PM
    Object reference can be used for invoking the object method. Simple names of method are appended to the object reference along with (.) dot operator. Within enclosing parentheses arguments are provided to the method. If any arguments are not required by the method empty parentheses shall be used.
    Java Code:
    objectReference.methodName(argumentList);
    or
    objectReference.methodName();
    Two methods are linked to Rectangle class: getArea() for the computation of the rectangular ...
    Categories
    Tutorial
  15. Referencing an Object's Fields

    by , 04-26-2012 at 06:08 PM
    Names are used to access the object fields. An unambiguous name shall be used.

    Simple name could be used for the field, within class. For instance, a statement could be added in Rectangle class, which will be priting the height & width.
    Java Code:
    System.out.println("Width and height are: " + width + ", " + height);
    In this case simple names are height & width.

    Code that is being present out of the object’s class may utilize ...
  16. Initializing an Object

    by , 04-26-2012 at 06:06 PM
    Point class code is as following:
    Java Code:
    public class Point {
        public int x = 0;
        public int y = 0;
        //constructor
        public Point(int a, int b) {
            x = a;
            y = b;
        }
    }
    Single constructor is present in this class. One may do the recognition of the constructor as same name is used by the declaration as the class does & no return type is there. Constructor present will be taking 2 integer ...
    Categories
    Tutorial
  17. Instantiating a Class

    by , 04-26-2012 at 06:03 PM
    A class is instantiated by the new operator, by memory allocation for new object and then making reference return to the memory. Object constructor is invoked by the new operator.

    A postfix & single argument is required by the new operator: call to constructor. Constructor’s name gives the class name to be instantiated.

    A reference is returned by the new operator to the object which has been created. Usually, reference is being assigned to the appropriate type variable. ...
    Categories
    Tutorial
  18. Declaring a Variable to Refer to an Object

    by , 04-26-2012 at 06:02 PM
    Now declaration of variable has been learned. You write:

    type name;

    It shows that the compiler which will be used name to refer, to the data, whose type will be type. Proper memory amount is also reserved by the declaration, for the variable.

    Reference variable could also be declared at its own lines. Like, for example:

    Point originOne;

    When originOne is declared like this, value will stay un-determined till the actual creation ...
    Categories
    Tutorial
  19. Inner Classes

    by , 04-26-2012 at 06:01 PM
    Inner class has a link with enclosing class instance just the way with instance methods & variables. It also has a direct access to fields and methods of the object. Because of the association of the inner class with an instance, static member cant be defined by it.

    Inner classes instances i.e. object are present in outer class instance. Let us consider these classes:
    Java Code:
    class OuterClass {
        ...
        class InnerClass {
            ...
        }
    ...
    Categories
    Tutorial
  20. Static Nested Classes

    by , 04-26-2012 at 05:58 PM
    A static nested class also has a link with the outer class just the same way with class methods & variables. Just like static class methods, it is not possible for the static nested class to directly refer to the instance variables which have been defined in the enclosing class. This means that they can be used just via object reference.

    Static nested classes can be accessed by the enclosing class name:
    Java Code:
    OuterClass.StaticNestedClass
    E.g, Syntax is ...
    Categories
    Tutorial
  21. Why Use Nested Classes?

    by , 04-26-2012 at 05:57 PM
    Many compelling reasons are present to use the nested classes. Among them:

    • Grouping of classes is done logically which are just used at 1 place.
    • Encapsulation is increased
    • Nested classes might be leading to more maintainable & readable code.



    Logical classes groupings—When one class is useful towards just 1 class, it would be so logical to do its embedding in that particular class so that to keep them together. They are known as the helper classes ...
  22. Nested Classes

    by , 04-26-2012 at 05:56 PM
    A class can be defined without other class by the Java programming language. Suck kind of class is known as nested class, which has been illustrated below:
    Java Code:
    class OuterClass {
        ...
        class NestedClass {
            ...
        }
    }
    
    class OuterClass {
        ...
        static class StaticNestedClass {
            ...
        }
        class InnerClass {
            ...
        }
    }
    Enclosing class ...
    Categories
    Tutorial
  23. Initializing Instance Members

    by , 04-26-2012 at 05:55 PM
    Usually code is being put for initialization of the instance variable, in constructor. Alternate ways present for initialization of the instance variables by using the constructor are: Final methods & Initializer blocks.
    Instance variable’s initialize blocks look same as the static initializer blocks, but the static keyword is not present in it:
    Java Code:
    {
        // whatever code is needed for initialization goes here
    }
    Initializer blocks are being copied ...
    Categories
    Tutorial
  24. Static Initialization Blocks

    by , 04-26-2012 at 05:53 PM
    A static initialization block is considered to be a normal code block which is enclosed in braces, { }. It is however preceded, by the static keyword. For example:
    Java Code:
    static {
        // whatever code is needed for initialization goes here
    }
    A class may consist of any static initialization blocks number.They might be present in the class body, anywhere. It is guaranteed by the runtime system to call static initialization blocks in similar order in which they ...
    Categories
    Tutorial
  25. Initializing Fields

    by , 04-26-2012 at 05:52 PM
    For a field, initial value could be provided often in its declaration:
    Java Code:
    public class BedAndBreakfast {
    
        // initialize to 10
        public static int capacity = 10;
    
        // initialize to false
        private boolean full = false;
    }
    It would be working well when value of the initialization is present & can be placed at 1 line. But certain limitations are associated with such initialization due to its simplicity. If some ...
    Categories
    Tutorial
  26. Class Methods

    by , 04-26-2012 at 05:51 PM
    Static variables as well as static methods are supported by the Java programming language. Static methods which consists of the static modifiers shall be invoked along with the class name with no need to create the class instance, as in
    Java Code:
    ClassName.methodName(args)
    One of the most common use, for the static method, is the static file access. For instance, static method would be added to the Bicycle class for accessing static field of numberOfBicycles.
    Java Code:
    public
    ...
    Categories
    Tutorial
  27. Class Variables

    by , 04-26-2012 at 05:44 PM
    When objects are made from the similar class blue print it means that every one of them consist of their distinct instance variables copies. In Bicycle class case, instance variables are speed, gear and cadence. E very Bicycle object consist of values of its own, for these variables which are present at different locations of memory.

    Sometimes one wishes to possess variables which are common, to all objects. This is usually done by the static modifier. Fields which consist of static ...
    Categories
    Tutorial
  28. Controlling Access to Members of a Class

    by , 04-26-2012 at 05:43 PM
    It is determined by the Access level modifier that a particular field or method could be used or invoked by the other classes or not. Access control levels are as following:
    • At the top level, which is package-private or private i.e no explicit modifier.
    • At the member level, this is public, protected, private or package-private i.e. no explicit modifier.



    With modifier public, declaration of the class might be done. In that case class becomes visible to classes, ...
    Categories
    Tutorial
  29. Using this with a Constructor

    by , 04-26-2012 at 05:41 PM
    Within constructor, this keyword could also be used to call the other constructor present in same class. This is known as explicit constructor invocation. Other Rectangle class present here along with varying implementations from that which is present in Objects section.
    Java Code:
    public class Rectangle {
        private int x, y;
        private int width, height;
            
        public Rectangle() {
            this(0, 0, 0, 0);
        }
        public Rectangle(int
    ...
  30. Using this with a Field

    by , 04-26-2012 at 05:40 PM
    The this keyword usage has a reason that field gets shadowed by constructor parameter or method.

    For example the Point class is as following written:
    Java Code:
    public class Point {
        public int x = 0;
        public int y = 0;
            
        //constructor
        public Point(int a, int b) {
            x = a;
            y = b;
        }
    }
    However, other way to write it is;
    Java Code:
    public class Point {
    ...
Page 1 of 2 12 LastLast