Solved

Vector instantiation

Posted on 2004-10-04
5
371 Views
Last Modified: 2011-09-20
Hi,

 I would like to know the difference between
 instantiation or initialization of Vector in Constructor and initializing as instance variable.

for eg:

case A
------
class x
{
       Vector v = new Vector();
       x()
      {
              // some initialization
      }
}

And
case B
------
class x
{
       Vector v = null;
       x()
      {
              v = new Vector();
              // some initialization
      }
}

Same with Array list also,
Which is better, is there any performance or any other issues declaring it in case B with respect to case A.

regards,
alpjose
0
Comment
Question by:alpjose
  • 3
  • 2
5 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12223685
i reckon the first one is best as it removes any confusion about what the initial value of the variable is.
0
 

Author Comment

by:alpjose
ID: 12223697
I would like to understand it more clearly, so i would ask u another question.
If i initialise it to null, then will there be any confusion??
0
 
LVL 92

Expert Comment

by:objects
ID: 12223715
In case B it is initialized to null initially, and then later it is changed to reference a new empty Vector (which is it's intended initial value). So theres a block of code where it is null even though that is not it's intended initial value.

Its a minor point but I just like to initialize any variable as soon as possible.
0
 

Author Comment

by:alpjose
ID: 12223752
Will it have any performance issue??
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 12223777
no performance differences
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Custom Java Application connects to database: Which one of the 20? 4 41
jboss 7.1 start up error 1 59
CSV file parsing thru Java 13 39
Java List 4 32
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question