Solved

Vector instantiation

Posted on 2004-10-04
5
363 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Will it have any performance issue??
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
Comment Utility
no performance differences
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
countAbc challenge 9 49
mapShare challenge 13 67
Eclipse IDE - Cannot copy/paste from console output 8 58
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now