• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initializing the Vector

 
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
either i do like this

1)



or

2)


although GetChildRecords value will definitely initialize it's value. the compiler doesn't complains about initializing the Vector.

but still this question is always in my mind, which approach should i follow 1 or 2.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a third approach, of course, which is to do it all in one line:

Vector<DBRecord> aoEmsEntManList = oEmsEntDBClass.GetChildRecords("employee");

If you have to do it in two lines, then that's surely because you need to initialize inside a try block, but use the variable outside, the try block, right? In that case, if the method throws an exception, then the variable won't be initialized, and so that's what the compiler is complaining about. In that case, you must use the "initialize to null" version. Otherwise, if the compiler doesn't force you to, then don't add the extra assignment; it just generates a little more bytecode for no reason. The Java compiler is very good at figuring out whether or not this is needed.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply.

yes i am using it inside the try block

SnapShot of the code-



1) As the code is in try and catch block. there may be chances that this method throws exception.
2) the compiler is not forcing me.
3) Also if i am intializing the value to null. Suppose my getChildRecords method dosen't return anything.
then it would exception to me at for loop.

Can i trust the compiler as it is not complaining. and should not use null ?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Amandeep Singh wrote:
Can i trust the compiler as it is not complaining. and should not use null ?



If the compiler does not force you to initialize the variable, then it's impossible for any code to access it without the assignment succeeding.

A method in Java can't "not return anything" -- either it returns a value (which could be null, depending on the method) or it throws an exception. If it throws an exception, then it's as if the statement was never executed, and the variable you're assigning the value to is not touched. But if the method returns, then the return value is always assigned to the variable.

If your method can return null, then regardless of this discussion, you must include code to handle that possibility -- i.e., testing for null, and skipping code or reporting an error.
 
Amandeep Singh
Ranch Hand
Posts: 856
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic