Java
  Home arrow Java arrow Page 2 - Java Part 4: Objects and Information Hidin...
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

Java Part 4: Objects and Information Hiding
By: Chris Noack
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2001-12-12

    Table of Contents:
  • Java Part 4: Objects and Information Hiding
  • Objects and "this"
  • Enforcing information hiding
  • The private and public declarations
  • Conclusion

  • 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


    Java Part 4: Objects and Information Hiding - Objects and "this"


    (Page 2 of 5 )

    In the last article, we used the Number class to represent a simple object (remember, an object represents an instance of a class). In the class’s main() function, only one instance of the class was created:

    public static void main( String[] args ){

    Number number = new Number(); // single instance of Number class

    number.addTo( 5 );

    number.divideBy( 5 );

    System.out.println( +number.getVal() );

    System.exit(1);

    }


    We could create more instances of the Number class like so:

    Number number2 = new Number( 4 );

    This would create a new Number object called number2, with its “value” data member being set to 4. If we then used number2’s addTo() method,

    number2.addTo( 5 );

    then number2’s value would increase to 9, and number’s value would remain unchanged, at 1.

    As you can see from the examples above, we can have multiple instances of a single class that are entirely independent. This can be quite handy, and, if needed, we can pass on instance of a class to another. Let’s demonstrate this by writing another method for the Number class:

    void addTwoNumbers( Number num ){

    this.value += num.value;

    }


    This method works on the object that it is a member of. The “this” statement represents the object that this method is a part of. Now, let’s demonstrate this function:

    number.addTwoNumbers( number2 );

    would perform the operation:

    number.value = number.value + number2.value;

    Now that I’ve described how to create a method for a class that will operate on itself, try writing similar methods for multiplying, subtracting and dividing from another Number class.

    More Java Articles
    More By Chris Noack


     

    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 6 hosted by Hostway
    Stay green...Green IT