Demonstration of the Pointers and References Concept : Declare Object « Class Definition « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Class Definition » Declare Object 
5.16.3.Demonstration of the Pointers and References ConceptPrevious/Next
/*
 *     file: PointersAndReferences.java
 *  package: oreilly.hcj.review
 *
 * This software is granted under the terms of the Common Public License,
 * CPL, which may be found at the following URL:
 * http://www-124.ibm.com/developerworks/oss/CPLv1.0.htm
 *
 * Copyright(c) 2003-2005 by the authors indicated in the @author tags.
 * All Rights are Reserved by the various authors.
 *
########## DO NOT EDIT ABOVE THIS LINE ########## */


import java.util.Vector;

/**  
 * Demonstration of the Pointers and References Concept.
 *
 @author <a href=mailto:kraythe@arcor.de>Robert Simmons jr. (kraythe)</a>
 @version $Revision: 1.3 $
 */
public class PointersAndReferences {
  /** 
   * Main method.
   *
   @param args Command Line arguments.
   */
  public static final void main(final String[] args) {
    Vector source = new Vector();
    source.add("O'Reilly");
    source.add("XML");
    source.add("Java");
    System.out.println("Original Vector");
    System.out.println(source.toString());
    // -- Try the first method. 
    someMethod(source);
    System.out.println("After first method.");
    System.out.println(source.toString());
    // -- Try the second method. 
    someOtherMethod(source);
    System.out.println("After second method.");
    System.out.println(source.toString());
  }

  /** 
   * Try using a vector without changing it.
   *
   @param source The source Vector.
   */
  public static void someMethod(Vector source) {
    Vector target = source;
    target.add("Swing");
  }

  /** 
   * Try using a vector without changing it with final parameter.
   *
   @param source The source Vector.
   */
  public static void someOtherMethod(final Vector source) {
    Vector target = source;
    target.add("JFC");  // ouch, still changes v.
    // source = new Vector(); // compiler error.
  }
}

/* ########## End of File ########## */
5.16.Declare Object
5.16.1.Using Objects
5.16.2.Demonstrates that all classes descend from the type java.lang.Object
5.16.3.Demonstration of the Pointers and References Concept
5.16.4.Implements a manager of lists that stores the lists by key
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.