View RSS Feed

Java Object

Object-oriented programming in Java.

  1. Enum Types

    by , 04-26-2012 at 05: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
  2. Passing Reference Data Type Arguments

    by , 04-26-2012 at 05: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
    ...
  3. Passing Primitive Data Type Arguments

    by , 04-26-2012 at 05: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()
    ...
  4. Parameter Names

    by , 04-26-2012 at 05: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
  5. Arbitrary Number of Arguments

    by , 04-26-2012 at 05: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
    ...
  6. Parameter Types

    by , 04-26-2012 at 05: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
  7. Passing Information to a Method or a Constructor

    by , 04-26-2012 at 05: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
  8. Naming a Method

    by , 04-26-2012 at 05: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
  9. Defining Methods

    by , 04-26-2012 at 05: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
  10. Declaring Member Variables

    by , 04-26-2012 at 05: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
  11. Declaring Classes

    by , 04-26-2012 at 05: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
  12. Calling an Object's Methods

    by , 04-26-2012 at 05: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
  13. Referencing an Object's Fields

    by , 04-26-2012 at 05: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 ...
  14. Initializing an Object

    by , 04-26-2012 at 05: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
  15. Instantiating a Class

    by , 04-26-2012 at 05: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
  16. Declaring a Variable to Refer to an Object

    by , 04-26-2012 at 05: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
  17. Inner Classes

    by , 04-26-2012 at 05: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
  18. Static Nested Classes

    by , 04-26-2012 at 04: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
  19. Why Use Nested Classes?

    by , 04-26-2012 at 04: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 ...
  20. Nested Classes

    by , 04-26-2012 at 04: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
  21. Initializing Instance Members

    by , 04-26-2012 at 04: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
  22. Static Initialization Blocks

    by , 04-26-2012 at 04: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
  23. Initializing Fields

    by , 04-26-2012 at 04: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
  24. Class Methods

    by , 04-26-2012 at 04: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
  25. Class Variables

    by , 04-26-2012 at 04: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
  26. Controlling Access to Members of a Class

    by , 04-26-2012 at 04: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
  27. Using this with a Constructor

    by , 04-26-2012 at 04: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
    ...
  28. Using this with a Field

    by , 04-26-2012 at 04: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 {
    ...
  29. Returning a Class or Interface

    by , 04-26-2012 at 04:39 PM
    When a class name is used by a method as its return type, like whosFastest would be doing, typeís class of returned objects shall be subclass or exact class of return type. Letís say that class hierarchy is there where java.lang.Number has a subclass ImaginaryNumber that is also the Objectís subclass that has been illustrated in figure given below:

    Name:  1.jpg
Views: 276
Size:  3.1 KB

    ImaginaryNumberís class hierarchy

    Letís say a method has been declared so that ...
    Categories
    Tutorial
  30. Returning a Value from a Method

    by , 04-26-2012 at 04:36 PM
    To the code, a method is returned which invokes it, when:

    • In method, all statements are completed.
    • reaches to the a return statement or
    • an exception is thrown (covered later)



    Which ever would come first?

    Return type of the method is declared in the method declaration. Within methodís body, return statement is used so that to return value.

    Method which has been declared void will not return value. Return statement is not need ...
    Categories
    Tutorial
Page 2 of 3 FirstFirst 123 LastLast