Java
  Home arrow Java arrow Page 2 - New Object Initialization in Java
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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

New Object Initialization in Java
By: Katie Gatto
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2008-08-19


    Table of Contents:
  • New Object Initialization in Java
  • What is an Object in the Java language and why would I need to make one?
  • Coffee example
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    New Object Initialization in Java - What is an Object in the Java language and why would I need to make one?
    ( Page 2 of 4 )

    In the Java language an object is a term that represents a space of memory that is bundled with code that will make changes to that memory. In this space is the "state" of the memory, which is the value of the variables that are built into that section of the code. The value of these variables allow for a great deal of flexibility of the code, because the meaning of the variables' values can change, or evolve over time.

    So what is Initializing an object?

    Initializing an object is a way of managing the memory. You need to do this in order to be sure that the memory is used correctly by the program. If the memory is not used correctly by the program, your objects will behave in unpredictable and undesirable ways, and your program will not behave in way that you want it to. If you want your program to act in a way that is in line with the code that you have actually written, then you need to initialize your objects, otherwise your program will not be worth the time it takes to write it.

    Is it really that important to Initialize my objects?

    Yes, this is an important part of coding in Java, and historically data that has not been properly initialized has been a common source of bugs, or flaws in the programs that can cause your object or even your program to become invalid and useless to the end user. This flaw was common in C, on which Java is based. It may not be sexy or fun, but it is pretty much mandatory. Unless, of course, you just have a wicked sense of humor and you want your program to mess with other people's heads.

    OK, OK, I believe you now, I need to know how to do this to write effective Java programs, I want to know how to do this. So, how exactly do I do this?

    Well, first, I have to admit that I played a little bit of a trick on you. When initializing an object you do not always have to explicitly state the object's initialization in your code. There is a set of default values that are used by Java when no value is stated by the programmer. So, if it happens that one of those values will work for your needs, then you will not have to write explicit code that tells the program to initialize in a certain way.

    Go ahead, send me that nasty e-mail, I'll wait; once you've vented we can get to discussing what those default values are.

    The default values are as follows:

    • For the Boolean type the default value is False.
    • For the byte type the default value is byte(0).
    • For the short type the default value is short(0).
    • For the int type the default value is 0.
    • For the long type the default value is OL.
    • For the char type the default value is u000.
    • For the float type the default value is 0.0f.
    • For the double type the default value is 0.0d.
    • For the object reference type the default value is null.

    Did you notice something about all of those default values? They are all essentially empty or off. So if emptiness is what you want, then using the default value is the way to go.

    If you want to use a default value, or to know if one was used (in case you need to review you code, or the work of someone else), you should be looking for a line of code that says this '// This class has no constructors or initializers; it should be in the line directly following the line that defined the source packet.

    You may have noticed that this line of code makes reference to two different things, an initializer and a constructor. These are both used to initialize the program. A constructor is similar to a method with only one major difference. Unlike a method, a constructor does not have any return type. It does however have a set of parameters and a body of code, just like a method. You will get to the tried and true method when we write the initialization. This is a method, and it creates what is known as an Instance Initialization Method, which you must have one of for each method. The Instance Initialization Method is based on each of the individual methods.

    To write a constructor you first define the class they define as public or private, and add your specifications.



     
     
    >>> More Java Articles          >>> More By Katie Gatto
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT