Solved

Vector instantiation

Posted on 2004-10-04
5
372 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

710 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