Your Federal Quarterly Tax Payments are due April 15th Get Help Now >>

The Meaning of Static by yurtgc548

VIEWS: 5 PAGES: 14

									The Meaning of
    Static
                         Review
   Assignment Operators (=, +=, -=, /=, *=, %= )
   Multiple Declarations ( int amount, numEmp;)
   Accumulating (sum=sum+ salary;)
   Prefix and Postfix Incrementing Operator
    n++;       ++n;          n++;          k=n++;
                             k=n;
   Syntax is the set of rules for creating grammatically
    correct language statements. The compiler will accept
    and not generate an error message. NOTE: Statements
    can be syntactically correct but still logically incorrect!
               Static Variables
   A variable declared as static belongs to the
    class as a whole.
   A static (class) variable is created only once, no
    matter how many objects of the class are created.
   Each object shares the same static variables.
   Changing a static variable’s value by a specific
    object changes that variable for all the objects.
   Since only one static variable is created, the same
    variable is shared by all instantiated objects.
public class Employee
{
private static int numEmployees;
private static double taxRate=.15;
private int idNum;
private String firstName;
private String lastName;
public Employee(int num)
{ idNum=num;             }
.
.
}
                      Scope
   The section of a program where the identifier
    is valid, is referred to as its scope.
   There are two types of scope, local (block) and
    class.
   Variable that are declared within a method are
    said to be local to that method.
   Class scope means these variables are
    available throughout the class.
                   Visibility and Scope
            Visibility is the determination whether the member can be
             accessed from outside the class, using the modifiers public,
             private or protected.

              public class Test
Visibility
Modifier
              {
              private double amount;            Class
                                                Scope
              private double price;
              public double total (double amt, double pr)
              { amount=amt;
                price=pr;            Local Scope
                return amt*pr; }
              }
          Lifetime and Duration
   The length of time that storage locations remain
    reserved for an identifier is referred to as both its’
    lifetime and duration.
   All data members that have class scope come into
    being when the class, within which they are
    declared,is loaded into the JVM and remain until the
    program finishes execution.
   Static variables have only class scope and have the
    same duration as their defining class.
   Local variables exist only while the block in which
    they are defined are in scope. Once the block goes out
    of scope, the storage areas are released.
             Final Variables
 Certain values in a class remain constant
  throught the class’ duration, these variables
  need to be declared as final.
 Final variables must be set to their value in

  their declaration.
public final static double PI = 3.1416;
public final static double ACCEL=32.2;
public final static double EULER = 2.71828;
     Static vs. Non-static Methods
   Methods that must be used with objects are
    referred to as non-static methods.
        objectName.methodName(arguments);
   A static method is one that does not operate
    on a object but receives all of its data as
    arguments. If it is public, it is considered to a
    general-purpose method.
        ClassName.methodName(arguments);
                 Method Signature
The first line of the method, or its header, is considered the
  method’s signature.

public static void calRate (int principal, double rate )

Visibility Modifier                       Argument List
            Scope Designation
                                 Return Type
                                               Method Name


 Method Overloading
        Algorithm Development
   Swapping values
   How do we switch the contents of two glasses?
   Create a basic swap method that swaps two
    integers.
   Add to your time class, create a method that
    swaps two time objects.
    Program Design and Development
   Problem: What is the result of tossing 100
    coins?
   Model: Create a coin class as a model.
   Example of things to model:
     Magic 8 Ball, Alarm Clock, Soda Machine
   Attributes: properties that are of interest
   Behaviors: define how the object reacts to its
    environment.
Rectangle Class
How would you model a rectangle?
What attributes do you need?
What behaviors does it have?
Break into groups:
Constructors (3)
Accessors (4): One for each data
.
member, all at once, and toString
method.
Mutators (3)
Other Methods (4)
Assignment: Create a Coin class.

 Data members, Constructors,
 Accessors, Mutators, and Other
 Methods.
 Coin Class
     side (head or tail)
     Toss the coin

								
To top