?
Solved

java basic question

Posted on 2005-03-07
8
Medium Priority
?
149 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
[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
8 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month10 days, 11 hours left to enroll

765 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