Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java encapsulation gotchas

JavaFAQ Home » General Java Go to all tips in General Java


Bookmark and Share

... When you return a Char, others can alter the Char they received without modifying the Char, as stored in your application, because Java uses pass by value to ...

Encapsulation is one of the tenets of object-oriented programming.

It states that objects should hide their inner workings from external view. Good encapsulation increases code modularity by preventing objects from interacting with each other in unexpected ways, which in turn makes future development efforts and refactoring easier.

Getters and setters

The first step toward building objects with good encapsulation is to have the getter/setter pair access private data fields. Requiring other objects that want to read and write fields in your object to do so through a mechanism you control allows you to enforce legal values and general internal data consistency.

If you have a field named duration that is expected to hold a positive integer, you can throw an IllegalArgumentException when another object tries to setDuration(-4).

If you’ve left your duration member public, you can’t prevent someone from calling yourObject.duration=-4 and messing up the internal consistency of your data.

Immutability of return values

The data that your object makes available through method return values should not be modifiable in a way that alters the internal state of your object. Returning primitives provides no worry in this regard.

When you return a Char, others can alter the Char they received without modifying the Char, as stored in your application, because Java uses pass by value to return primitives.

However, when returning objects, you must be careful to monitor their mutability. When an object’s value cannot be altered after instantiation, it is said to be immutable.

Many of the objects in the java.lang package hierarchy are immutable, including String, Char, and Short. Making an object immutable requires three steps:
Read about it here at Builder.com


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy