Home arrow Java arrow Page 2 - Java Part 4: Objects and Information Hiding

Java Part 4: Objects and Information Hiding

In part three of this ten part tutorial, Chris discussed some of the primitive data types available in Java. He also went through a basic Java class. In this article, he will explore that class further and discuss some aspects of objects, as well as information hiding.

Author Info:
By: Chris Noack
Rating: 3 stars3 stars3 stars3 stars3 stars / 6
December 12, 2001
  1. · Java Part 4: Objects and Information Hiding
  2. · Objects and "this"
  3. · Enforcing information hiding
  4. · The private and public declarations
  5. · Conclusion

print this article

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() );



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.
blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials