Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

java basic question

Posted on 2005-03-07
8
Medium Priority
?
150 Views
Last Modified: 2012-05-05
Which method is better or correct in object oriented sense?
Method 1:
========
public class Bye {
      private Vector v = new Vector();
      public Bye() {
            v.add(new Integer(10));
      }
      public void print()
      {
            System.out.print(v.get(0));
      }
      public static void main(String args[])
      {
            Bye b = new Bye();
            b.print();
      }

}

OR should initialization occur only in the constructor?Like this

Method 2:
==========
public class Bye {
      private Vector v;
      public Bye() {
                v = new Vector();
            v.add(new Integer(10));
      }
      public void print()
      {
            System.out.print(v.get(0));
      }
      public static void main(String args[])
      {
            Bye b = new Bye();
            b.print();
      }

}
0
Comment
Question by:neekris
6 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 80 total points
ID: 13484242
Since this is a feature specific to Java, it cannot be completely answered in 'object-oriented' sense. C++ does not support the first method, as far as I remember.

To much of an extent, it is a matter of choice - what do you want to use? You should follow one approach rather than both - if you initialize some in the constructor and some outside, you might get confused and might as well forget to initialize some of them. So you should initialize all of them in one place, if possible. I prefer doing it in the constructor.

There are some cases when you would be bound to initialize in the constructor. For example, if you have a BufferedReader or something which you wanna initialize by calling its constructor and if its constructor throws an exception, you would need to put a try/ catch block around it or mark your method as 'throws' that exception.... in such cases, you will have to initialize it in your constructor only.... you can't do it outside.
0
 
LVL 92

Expert Comment

by:objects
ID: 13484257
I'd use method 1 on the basis of initialising variable asap.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13484278
Forgot to mention - the initialization outside will occur before the constructor.
0
Independent Software Vendors: 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!

 
LVL 13

Expert Comment

by:Webstorm
ID: 13484437
Hi neekris,

Both solutions make no difference in the compiled class.

But, the advantage of the 1st solution is when you have multiple constructors, you can avoid initializing a member variable multiple times, by doing it outside any constructor.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 13484444
Other constructors can always call the default constructor too ;-) hey, no arguments meant :-D
0
 
LVL 1

Expert Comment

by:anindyabecs
ID: 13485140
Hi neekris,
              Goin by Sun's specification you should use the second method only of initializing the Vector within the constructor (Sun suggests u to use minimum number of instance variables. In case u use it allocate memory allocation should be done in constructor or respective methods). This helps in better memeory handling as in doing it you will allocate the Vector memory ( memory for 10 objects will be allocated) once the object (Bye) is initialized.
              In this way u can use other constructors also which might not require the Vector to be initialized (as already suggested by Webstorm).
              Thanks -
                                  Anindya
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 19 hours left to enroll

564 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