The Basics


Properties | Terms | Classes | Methods | Data

PROPERTIES

This page was created as a complete tutorial through class, method, and data declarations and definitions, by using as formulaic an approach as possible from the get go to tie it all together. Although there are sure to be several exceptions to these rules, we shall gloss over these at first and mention each in turn later. With that said, then we may now be very general and assert the following:
All stuff in JAVA - by which I mean either classes (objects), methods, and data - has a certain number of things in common about them simply by virtue of their being stuff. In general, each of them will have
  • a scope in which it persists


  • a name


  • a signature


  • a way of declaring it


  • a way of defining it


  • ways of using it


  • TERMS

  • scope - scopes are always demarked by the curly braces, { and }. They always come in pairs, one opening and closing. Anything that appears between these matching curly braces is said to be in the scope of those braces, or whatever class or method or loop necessitated them.

    Scopes can be nested, which means that you can open up scopes inside of each other - you don't have to close one scope to start a new one. Anything that appears in a scope may be used inside of that scope, and all other nested scopes inside that scope as well. Things cannot, however, be used outside of their own immediate scopes.

    Here's an example

    .......
    :{    :
    :   a;:
    :}    :
    .......
    
    ...............
    :{            :
    :    b;       :
    :             :
    :    ........ :
    :    :{     : :
    :    :    c;: :
    :    :}     : :
    :    ........ :
    :             :
    :    ........ :
    :    :{     : :
    :    :    d;: :
    :    :}     : :
    :    ........ :
    :             :
    :}            :
    ...............
    
    .......
    :{    :
    :}    :
    .......
    a may be used in the scope of the red curly braces only*

    b may used in the scopes of the orange, green, and blue curly braces*

    c may be used in the scope of the green curly braces only*

    d may be used in the scope of the blue curly braces only*

    nothing is in the scope of the yellow curly braces


    *note: static methods do not have access to instance members, regardlesss of scope


  • persistence - a thing is said to persist when it available to be used by the computer program. In this way, persitence is sort of like the lifetime of that thing - after it comes into being, it will be around -or persist - for a short while and then it will "disappear" and no longer be used or called upon.

    The crucial point here is that a thing persists for as long as the computer has knowledge about or is operating within that thing's scope. As soon as execution leaves a scope, everything that was inside of it ceases to persit.



  • name - the name of a class, method, or data is used by the computer to figure out what class or method or data you are referring to. Like most people classes, methods, and data have full names as well as their short names. The fully qualified name of something contains the package name, if it exists in a package, and the name of the Class that it is in, followed by its own short name.

    Whenever you are in the same scope as the thing to which you are refering, then just the short name will suffice. However, if you are outside of the scope in which that thing was declared, then you must use a more qualified name.

    Here are some examples

    Say you have a Calculator class for computing the squares and the cubes of numbers, and you are using the java.lang.Math and java.util.Stack classes for some of your computation. Here is a table of how qualified the method name has to be depending upon where you are calling it from and what you are importing, if anything.

    method to call calling from imported name to use reason
    sqaure main class nothing Calculator.square the main class is outside of the class of square() which is declared in the Calculator class.
    square inside cube nothing square the method cube is in the class Calculator scope, so it can used from anywhere inside of that class. cube, being declared inside of that class, meets this requirement
    push main or Calculator classes nothing java.util.Stack.push the push method is in a class called Stack which is in a package called java.util. Having not imported anything, then you must use the fully qualified name to refer to the method from outside of the Stack class.
    push main or Calculaotr class java.util.*; or java.util.Stack; Stack.push Becasue the Stack class from the java.util package was imported, you only need to use the class name to append the method name, since you are still outside of the Stack class
    pow main class nothing Math.pow pow belongs to the Math class in the java.lang package, but since this package is automatically imported for every class, then you only have to append the method name with the class name

    And here is a link to a page that explains about naming conventions.



  • signature - something's signature is how the computer distinguished between things - can tell one thing from another. A signature usually takes into account the name, the types, and sometimes order of the types.

    The computer, when differentiating between things, will take into account all of these aspects. Since a name is only part of a signature, you can have multiple things with the same name as long as something else about their signature is different.

    Access modifiers are NOT included in signatures. This means that you can override constructors, methods, and data and change the visibility of the descendant member.



  • declaration - a line of code that decribes its unique signature and signals to the computer that you may be making use of it later on.



  • definition - the value or the decription of what the thing will be or have or contain - its actual the behavior.
  • CLASSES

    scope
    Most classes themselves are not actually in any scope. The are defined in a .java file with the same name as the class. However, it is possible to declare and define inner classes, which naturally would be inside of the first class.

    An instance of a class will persist as long as there is some reference to it, otherwise it will deallocated during garbage collection.

    Classes define scopes of their own, in which its members may be declared.

    Every class automatically in its scope, a this reference which points to itself.
    signature
    A class' signature includes it's (fully qualified) name, and its type (what it extends/inherits from). Remember that all classes inherit from the Object class.
    declaration
    <modifiers> class ClassName extends SuperClassName {}

      *note: if ClassName inherits from just Object, then "extends SuperClassName" is not necessary.
    definition
    < class declaration statement > {
        < data member declarations and initial assignments, if any >
                                  |
                                  |
                                 \|/
                                  '
        < contructor(s) >
               |
               |
              \|/
               '
        < method member declarations and definitions >
                              |
                              |
                             \|/
                              '
    }
    usages
    There is an exception for the usage of instance objects, and it involves how to properly call the constructor method of that class. An instance of a class may be obtained from one of the constructor methods by declaring a reference data of that Class type, and then assigning it a value. However, although the constructor method is called, the correct syntax for object instantiation is

        ClassName instanceName = new ClassName(<arguments>);         correct

    and not

        ClassName instanceName = ClassName.ClassName(<arguments>);         incorrect
    METHODS

    scope
    A method is always in the scope of some class definition. Therefore, any methods in a class may call any other methods in that class by name, without qualifying the class first.

    Methods define scopes of their own, in which local data may be declared, but they will not persist beyond the execution of that method. The parameters of a method also exist in this non-persistent scope.

    Class methods never have access to instance members.
    signature
    The signature of a method includes its (fully qualified) name and the order and the types of its parameter list.
    declaration
    < modifiers > < return type > methodName(< parameter list >) {}

    The return type can also be void, for non-returning methods.

    The parameter list is an ordered list separated by commas, of the type of the parameter and the local name that it will have in the method's scope. Parameter lists may be empty. The following are examples of parameter lists:

    ()
    (<type> name)
    (<type1> name1, <tpye2> name2, <type3> name3)
    definition
    < class declaration statement > {
        < any statements, including local data declarations and assignments >
                              |
                              |
                             \|/
                              '
        return returnData;
    }


    The last line is called the return statement. If the method has a void return type, then the return statement is ommitted.

    If the method would like to reference instance data explicitly, then the this keyword may be used. This is useful when a local variable name overrides a class variable. For example, many constructors and mutators are defined like this:

    setClassVariable(type classVariableName) {
        this.classVariableName = classVariableName;
    }


    If the method is a constructor, then the this reference may be used to call another constructor to perform the same actions without creating a new object. In this way, you can take advantage of code reuse. Here is an example:

    <modifiers> ClassName(<type1> classData1, <type2> classData2,
                          <type3> classData3) {
        this.classData1 = classData1;
        this.classData3 = classData2;
        this.classData2 = classData3;
    }
    
    <modifiers> ClassName(<type1> classData1, <type2> classData2,
                          <type3> classData3, <type4> classData4) {
        this(classData1,classData2,classData3);
        this.classData4 = classData4;
    }


    If the method is a constructor for a class that inherits from a superclass, then the keyword super may be used to reuse code in the the same way as the this keyword for constructors in the same class. Here's an example:

    <modifiers> SubclassName(<type1> classData1, <type2> classData2,
                          <type3> classData3) {
        super(classData1,classData2,classData3);
    }
    usages
    A method may be called from an outside class as such

    ClassName.methodName(< argument list >); If calling from within the same class, the class name qualifier is not necessry.

    When data is passed in as arguments to a method, the local parameters that the computer generates for that method's scope are copies of the original argument values. They are not the original data themselves. So methods cannot affect numerical arguments, becasue the method only has access to a copy of its value. When an object reference is passed to an argument list, the computer generates a copy of that reference. Thsi means that a method can modify the value of the orignal object, since it now has a reference to it. This scheme of argument-passing is called pass by value.
    DATA

    scope
    A data's scope, object reference type or primitive type, is the scope in which it was declared. This is either a class or instance data, or a local data in a method.
    signature
    A data's signature consists of its (fully quallified) name and its type.
    declaration
    The syntax for most data declaration is as follows:

        <modifiers> <tpye> dataName;

    However, literal constants, which are primitive types may be explicity declared and be ready for usage in expressions and evaluation on the spot. Here, 7 is a literal constant declared and used immediately in assignment:

        int a = 7;
    definition
    The definition of a variable is its value. Default values for primitive types are assigned immediately upon declaration. Reference data is assigned the keyword value null immediately upon declaration. This special value, like any other, may be used in boolean tests, in this case with reference datas.
    usages
    Data may be assigned values, assuming they were not declared as constants, with assignment statements, using the equal sign "=". Whatever is on the left of the equals sign is assigned the value of whatever is on the right.

    In the case of primitives, the values is simply copied. In the case of object references, the value of the reference on the left becomes the value of the reference on the right (the object). So at the end of this type of assignment statement, you will have two object references referring to the same object.

    Assignments can only occur between data of the same type. However, you may be able to take advantage of numeric promotion of explicit casting in some cases. For more about this, see this link regarding operations with differing numeric types.
    Last Updated:9/25/02