Java Object Initialization and Creation

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.

Please be aware that this tutorial assumes that you have a basic knowledge of Java terminology to begin with, and that you can already write some basic code. There is some information available for the Java novice, but if you are new to Java, and find that you are having problems following this tutorial, then you may need to spend a little bit of time with the basics of the language and then return to this tutorial once you are more familiar with the language.

Unless, of course, you can already code in C or C++, in which case you should be able to follow along with the tutorial without much of a problem. If you need to find a resource for learning the basics of Java, the author of this tutorial humbly suggests that you get it straight from the source. Sun Microsystems offers a solid basic set of lessons on Java. These are free and available online for the use of the public.

What is Java?

Java is a coding language that was released by Sun Microsystems in 1995, but initially developed by James Gosling in 1991. The language, which is based on C and C++, took a new twist on two of the languages that programmers were already very familiar with. Java was designed with more of a object orientation in mind.

Java became popular with programmers in part because of its object based focus, which allows them to focus on the actions that a specific event or occurrence will trigger in the program instead of having to draw attention to the overall framework of the program as in more traditional programming languages. Another reason it became popular is because Java is a cross platform language that can work on remote sources. It also has a high degree of scalability in the amount of security, which means that an applet can be very restrictive or very open in its security.

Since May of 2007 most of Java’s code has been deemed “open source,” which means that it is covered under the GNU General Public License, and is freely available for those who wish to develop using the Java language or platform. The average end user will have had the most experience with Java when they played an online game, viewed images in 3-D or used an application for chatting online.

{mospagebreak title=What is a Java Object and why would I initialize one?}

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.

{mospagebreak title=Java Object Creation Example using Coffee}

For example, if you wanted to create a virtual coffee cup, and then fill it with virtual coffee, your code would look like this:


// In Source Packet in ex2/CoffeeCup.java

// Approach 1

class CoffeeCup {

private int innerCoffee = 355;

// no constructors defined in this version of CoffeeCup

//…


Or an acceptable alternative version of the code to initialize your object could look like this:


// In Source Packet in ex5/CoffeeCup.java

// Approach 3

class CoffeeCup {

private int innerCoffee;

// Only two constructors defined in this

// version of CoffeeCup

public CoffeeCup() {

}

public CoffeeCup(int startingAmount) {

if (startingAmount < 0) {

String s = “Can’t have negative coffee.”;

throw new IllegalArgumentException(s);

}

innerCoffee = startingAmount;

}

//…


}

}


{mospagebreak title=Initializing a Java Object Summary}

Your lesson in how to initialize your very first object in a Java program has come to an end. We talked some theory, and took a look at some code, but the only way that you will know if you can do it is to go out and try it.

If you want you can give the coffee example a try, but that’s really limited in usefulness for you as a programmer. Instead, I suggest you give making and initializing your own object a try.

If you do this, you will know whether you can create and not just imitate. Don’t be scared, you know that you want to give it a try. So what are you waiting for? Open up your compiler and make yourself a program, and initialize some Java objects!.

I’d like to mention one important point: I did not originally create the code examples I used. I adapted them from some work that came from an item posted on Java World in 1998.

You can view the original work online at http://www.javaworld.com/jw-03-1998/techniques/techniques.zip. All code snippets from this example come from the mind of the author of that example, and I wish to give credit where it is due.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye