This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What will make better efficient?

 
arun mahajan
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
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?
regards,
arun
[ April 04, 2002: Message edited by: Cindy Glass ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Corey
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
Corey
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic