JSP Tutorials

Good Practices : Programming Tips Java

Article by Farhan Khwaja

One of the Good practices while programming using Java is listed below.

Consider Static Factory Methods instead of Constructors

The normal way for a client to obtain an instance of itself is to provide a public constructor. But apart from this their is another technique which should be the part of every programmer’s toolkit. A class can provide a static factory method, which is static method which returns the instance of the class.

Example :

The below method translates a boolean primitive value into Boolean object reference.public static Boolean valueOf(boolean b){ return b ? Boolean.TRUE : Boolean.FALSE;}

Note :

A class can have static method instead of, or in addition to, constructors.

Advantages of using static factory methods :1. Unlike Constructors, static-factory methods have names :

If the parameters to a constructor do not, in and of themselves, describe the object being returned, a static method with a well chosen name is easier to use and the resulting client code easier to read. For example, the constructor BigInteger(int, int, Random), which returns a BigInteger that is probably prime, would have been better expressed as a static factory method named BigInteger.probablePrime.

2. Static factory method do not require to create a new object each time they are invoked :

This allows immutable classes to use preconstructed instances, or to cahce instances as they are constructed as to avoid creating unnecessary duplicate objects. The Boolen.valueOf(boolean) method illustrates this technique; it never creates an object. This can greatly improve performance.

3. Static factory methods can return an object of any subtype of their return type :

This gives us the flexibility of choosing the class of the returned object. To understand the above advantage better lets take an example :

The class java.util.EnumSet introduced in 1.5 release, has no public constructor, but only static methods. They return one of the two implementations depending upon the size of the underlying enum type; if it has 64 or fewer elements, the static factories return RegularEnumSet instance, which is backed by single long; if enum type had 64 or more elements, the factories return JumboEnumSet instance, backed by a long array.

The existence of the two implementations are invisible to the client. So a future release could add third or fourth implementation with a different returned value.

4. Static factory methods reduce the verbosity of creating the parameterized type instances :

Unfortunately, we must specify the the type parameters when we invoke the constructor of a parameterized class even if they are obvious from context. This makes us provide the type parameters twice in quick succession :

Example :

Map<String, List<String>> m= new HashMap<String, List<String>>

This redundant specification quickly becomes painful as the length and complexity of the type parameter increase. But with static methods, the compiler can figure out the type parameters for us. This is called type inference. For example,

public static <K, V> HashMap<K, V> newInstance(){ return new HashMap<K, V>();}

The above statement can reduce the verbosity :

Map<String, List<String>> m=HashMap.newInstance();

Follow me on Code 2 Learn

Farhan Khwaja is a young Entrepreneur and professional blogger from Mumbai, India. He writes about programming languages, programming tutorials, tips and tricks and also writes about topics of Computer Science.

Code 2 Learn primarily focuses on Computer Sciecne, Programming Tips and tricks, Tutorials etc.










Ultimate Guide To Job Interview Answers
New: Higher Pricepoint & It Converts Even Better! You Make Over Per Sale With The 61% Commish. Successful Product On CB For 7 Years Now. Large “hungry Crowd” Of Buyers Always There. If You Have A Jobs Or Career Site, This Is A Winner.
Ultimate Guide To Job Interview Answers

Java In A Nutshell, 5th Edition

With more than 700,000 copies sold to date, Java in a Nutshell from O’Reilly is clearly the favorite resource amongst the legion of developers and programmers using Java technology. And now, with the release of the 5.0 version of Java, O’Reilly has given the book that defined the “in a Nutshell” category another impressive tune-up.In this latest revision, readers will find Java in a Nutshell, 5th Edition, does more than just cover the extensive changes implicit in 5.0, the newest version of Java

List Price: $ 44.95

Price: [wpramaprice asin="0596007736"]


Flash Templates – Basic Advantages of its Use

Article by Nandini Sen

Flash based technology is highly used for enhancing the interactivity of a site. It is kind of a multimedia platform with which an expert can create magic on the pages of a website. Mainly used for creating integrated video, advertisement, animations and web components.

Nowadays most of the web browsers are compatible to view flash based web sites. Many mobile phones also support this application. The flash template designs have become very popular and are widely used by the designers for some advantages.

* Accessibility: The flash templates can be viewed with a variety of devices and also through many systems like Solaris, Windows, Pocket PC, Linux etc. A number of output devices also support its accessibility. * Plug in and Format: The flash player takes very little memory for its installation. This is considered an advantage compared to other plug-ins like Acrobat Reader and Java. Compared to video clips, the vector graphics teamed with programming code takes smaller memory. * Widely Used: Even the open source projects use the flash templates. Flash templates are simply used to enhance the look and feel of the ordinary sites giving it a professional look. SWF files can also be created with this application. These SWF files are used for converting the text files to graphics.

The flash templates are easy to design and the application is quite user friendly. This application is widely preferred by the designers who use them to create a flash based environment to their site. If you are looking for efficient flash designers to change the appearance of your site or develop a completely new site with flash based application, you can contact us. We are Flash Design India – a reputed company offering affordable flash web design services. Get fresh and unique flash web templates for your sites.

Nandini Sen is a content writer and presently writing for Flash Design India. It is a flash design company providing flash header design, flash banner, flash web design etc.










Inyourpjs.com
Work At Home Job Guide And Resource Manual. Single Mother Of 3 Formerly On Welfare And With No Computer Or Special Skills Exposes Her Incredible Secrets On How To Make Money With More Real Work-at-home Jobs Than You Can Ever Handle…
Inyourpjs.com

Aunt Martha’s Java Time Kaleidoscopes, Full Color Iron On Screenprinted Patterns, 2 Coffee Themes

  • Contains 2 different coffee themed patterns
  • Contains optional iron on glitter highlights
  • Images are approximately 6-inch by 6-inch
  • Fully machine washable after ironing on
  • Easy to follow instructions lets you complete your project in only a couple minutes

Kaleidoscopes Full Color Iron-On Transfer Patterns. Create beautiful dish towels and other home decor items with these authentic screen printed transfer patters. Each package contains two 6×6″ transfers in vibrant colors, optional glitter accents for each image and detailed instructions. Made in USA.

List Price: $ 7.49

Price: [wpramaprice asin="B0029SABIM"]

creating code template in eclipse ide in java program
Video Rating: 0 / 5


  • Copyright © 1996-2010 Programming tutorials for beginners,. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress