Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Vector instantiation

Posted on 2004-10-04
5
Medium Priority
?
377 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 200 total points
ID: 12223777
no performance differences
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

963 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