Solved

Vector instantiation

Posted on 2004-10-04
5
370 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

860 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