• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructing java objects

 
Ad Criag
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic