Home arrow Java & J2EE arrow Page 2 - Java Object Initialization and Creation

What is a Java Object and why would I initialize one? - Java

Initializing your objects is an important part of coding in Java, if you care at all about managing your memory. You will also find that properly initialized object creation leads to fewer problems with bugs. This article will explain what objects are in Java, why you should initialize them, and how to accomplish the task of safely creating Java objects.

TABLE OF CONTENTS:
  1. Java Object Initialization and Creation
  2. What is a Java Object and why would I initialize one?
  3. Java Object Creation Example using Coffee
  4. Initializing a Java Object Summary
By: Katie Gatto
Rating: starstarstarstarstar / 10
August 19, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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 & J2EE Articles          >>> More By Katie Gatto
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: