It's not a secret anymore!
The moose likes Beginning Java and the fly likes Constructing java objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructing java objects" Watch "Constructing java objects" New topic

Constructing java objects

Ad Criag

Joined: Jun 27, 2007
Posts: 1
Why are some java objects constructed in this manner:

objectype objectname = new objecttype

but others like this :

objectname = new objecttype

ie the object type is not specified on the left hand side of the =
Srikanth Ramu
Ranch Hand

Joined: Feb 20, 2007
Posts: 76
Your second option is not a valid one and you need to specify the type either in new objecttype() statement or separtely. C
heck your code you should find statments like:

Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
Hey Criag,

First things first. All objects are basically created in new <Class_Name>() fashion.

Now coming to your point it is the initialization. In case

You create a new object and then store the reference to the newly created object, there by initializing var_name.

But in cases where the initialization may happen based on certain runtime conditions.

Hope this makes your understanding clear.

[ June 27, 2007: Message edited by: Pradeep Kadambar ]
Katrina Owen

Joined: Nov 03, 2006
Posts: 1367
Originally posted by Ad Criag:
objectname = new objecttype

Sometimes you know that you need a variable for an object of a specific type, but you don't know exactly what that object will be. You might then see code like this:

Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Well, what you have there is two different coding techniques to work with:

One, declare the class reference variable ahead of time, then instantiate it later, like this

public static void main( String [] args) {

ClassName myClass; // declares the variable myClass of type ClassName

// other lines....

myClass = new ClassName();

// the above line uses new to allocate a new class of type ClassName,
// and assign it's reference to the variable, myClass, whose type is
// already known to the compiler because it was declared earlier


Two, use one line to declare the variable AND instantiate the object:

public static void main(String [] args) {

ClassName myClass = new ClassName();

// This performs the same work but does it all in one source line.
// The first ClassName tells the compiler the type of the variable
// (myClass) the second ClassName tells the compiler what type of
// object to instantiate.


I think more a matter of style but then, certain coding standards may specify a preference.

(Edited to add) I had not caught Katrina's post and that is a good example. The dynamics of a particular design may well have you instantiating objects "on the fly" and you may not know what type until that point. So the one liner is a little more "source efficient". So in that case you might choose the one-liner. If you are initializing an environment to a known condition, you may want to declare variables up front and instantiate later.
[ June 27, 2007: Message edited by: Bob Ruth ]

SCJP - 86% - June 11, 2009
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
I would prefer:

even if I would know the real initialization would be later.

By doing this I will not have the famous NullPointerException at runtime if I forget the initialization later.

Also if I declare an object within a method I must initialize it, before using.

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Originally posted by Remko Strating:
I would prefer:

even if I would know the real initialization would be later.

There are cases where this is not possible, though. E.g., the way to get and use a connection in JDBC is like this:

The getConnection call must be inside the "try" block -because it can throw an exception- but "con" must be declared outside of it, because it's needed in the "finally" block.

This is just an example; you'd encounter this whenever an object is needed in the finally block, but must be created or retrieved in the try block.
I agree. Here's the link:
subject: Constructing java objects
It's not a secret anymore!