• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Constructing java objects

 
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 =
 
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:

 
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 ]
 
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:

 
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 ]
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • 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.
 
Rancher
Posts: 43016
76
  • 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.
 
The only thing that kept the leeches off of me was this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic