Java
  Home arrow Java arrow Page 9 - A Closer Look at Methods and Classes
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

A Closer Look at Methods and Classes
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2005-06-30

    Table of Contents:
  • A Closer Look at Methods and Classes
  • Overloading Constructors
  • Using Objects as Parameters
  • A Closer Look at Argument Passing
  • Returning Objects
  • Recursion
  • Introducing Access Control
  • Understanding static
  • Introducing final
  • Introducing Nested and Inner Classes
  • Exploring the String Class
  • Using Command-Line Arguments

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Closer Look at Methods and Classes - Introducing final


    (Page 9 of 12 )

    A variable can be declared as final. Doing so prevents its contents from being modified. This means that you must initialize a final variable when it is declared. (In this usage, final is similar to const in C/C++/C#.) For example:

    final int FILE_NEW = 1;
    final int FILE_OPEN = 2;
    final int FILE_SAVE = 3;
    final int FILE_SAVEAS = 4;
    final int FILE_QUIT = 5;

    Subsequent parts of your program can now use FILE_OPEN, etc., as if they were constants, without fear that a value has been changed.

    It is a common coding convention to choose all uppercase identifiers for final variables. Variables declared as final do not occupy memory on a per-instance basis. Thus, a final variable is essentially a constant.

    The keyword final can also be applied to methods, but its meaning is substantially different than when it is applied to variables. This second usage of final is described in the next chapter, when inheritance is described.

    Arrays Revisited

    Arrays were introduced earlier in this book, before classes had been discussed. Now that you know about classes, an important point can be made about arrays: they are implemented as objects. Because of this, there is a special array attribute that you will want to take advantage of. Specifically, the size of an array—that is, the number of elements that an array can hold—is found in its length instance variable. All arrays have this variable, and it will always hold the size of the array. Here is a program that demonstrates this property:

    // This program demonstrates the length array member.
    class Length {
     
    public static void main(String args[]) {
       
    int a1[] = new int[10];
       
    int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};
       
    int a3[] = {4, 3, 2, 1};
       
    System.out.println("length of a1 is " + a1.length);
       
    System.out.println("length of a2 is " + a2.length);
       
    System.out.println("length of a3 is " + a3.length);
     
    }
    }

    This program displays the following output:

    length of a1 is 10
    length of a2 is 8
    length of a3 is 4

    As you can see, the size of each array is displayed. Keep in mind that the value of length has nothing to do with the number of elements that are actually in use. It only reflects the number of elements that the array is designed to hold.

    You can put the length member to good use in many situations. For example, here is an improved version of the Stack class. As you might recall, the earlier versions of this class always created a ten-element stack. The following version lets you create stacks of any size. The value of stck.length is used to prevent the stack from overflowing.

    // Improved Stack class that uses the length array member.
    class Stack {
      private int stck[];
      private int tos;
     
    // allocate and initialize stack
     
    Stack(int size) {
        stck = new int[size];
        tos = -1;
     
    }
     
    // Push an item onto the stack
      void push(int item) {
        if
    (tos==stck.length-1) // use length member 
          System.out.println("Stack is full.");
        else
          stck[++tos] = item;
      }
     
    // Pop an item from the stack
      int pop() {
       
    if(tos < 0) {
          System.out.println("Stack underflow.");
          return 0;
        
    }
        else
          return stck[tos--];
      }
    }
    class TestStack2 {
     
    public static void main(String args[]) {
        Stack mystack1 = new Stack(5);
        Stack mystack2 = new Stack(8);
        
    // push some numbers onto the stack
        for(int i=0; i<5; i++) mystack1.push(i);
        for(int i=0; i<8; i++) mystack2.push(i);
        
    // pop those numbers off the stack
        System.out.println("Stack in mystack1:");
        for(int i=0; i<5; i++)
         
    System.out.println(mystack1.pop());
        
    System.out.println("Stack in mystack2:");
        for(int i=0; i<8; i++)
          System.out.println(mystack2.pop());
      }
    }

    Notice that the program creates two stacks: one five elements deep and the other eight elements deep. As you can see, the fact that arrays maintain their own length information makes it easy to create stacks of any size.

    More Java Articles
    More By McGraw-Hill/Osborne


     

    Buy this book now. This article is excerpted from chapter 7 of Java: the Complete Reference, J2SE 5 Edition, written by Herbert Schildt (McGraw-Hill/Osborne, 2004; ISBN: 0072230738). Check it out at your favorite bookstore. Buy this book now.

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT