File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes What will make better efficient? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What will make better efficient?" Watch "What will make better efficient?" New topic

What will make better efficient?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 305
If I write the following class in two style:
Style A:

Style B

Which is better way of decalring xx object and why? or this is soley depend upon the requirement?
[ April 04, 2002: Message edited by: Cindy Glass ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
It's important to remember that initialization of instance variables is not the same as executing a constructor. In the case you gave, the results would probably be the same either way. However, consider what would happen if you had multiple constructors:

In such a case, it would probably be better to initialize the member xx outside of the constructors because, otherwise, you're simply creating extra code.

SCJP Tipline, etc.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Just as a side-note, there a many intricacies to initializing a class. Take a look at the JLS, §12.4 Initialization of Classes and Interfaces or do a search in the Programmer Certification Forum to get all sorts of juicy details.
Hope that helps,
I agree. Here's the link:
subject: What will make better efficient?
It's not a secret anymore!