• Post Reply Bookmark Topic Watch Topic
  • New Topic

Will Initialization of HashMap on Different Place make any Difference?  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I Have one question in Java

public class HashMapTest {


private static HashMap mp;

public static void main(String[] args) {
// TODO Auto-generated method stub
mp=new HashMap<String, String>();

}

}

This is My Code if I create Object in two place
1)Outside main() method but within class.
2)Inside main() method like above

Will these two approach makes any difference ?

I am asking a general senerio for creating any Type of Objects Primitives or References?

Thanks
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference that this makes is that, that variable will be initialized every time that method is called (I know in this case it's a main method, but I'm talking about the general case) unlike if you have initialized it where the static variable is declared.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So Initializing Each time will create any Overload on program efficency or unrfrence Object will be GC(Gargae Collected)?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote: . . .
This is My Code if I create Object in two place
1)Outside main() method but within class.
2)Inside main() method like above
. . .
If you mean does it make any difference whether you create the object in one of those two places, that has already been answered.
If you really mean create it in two places, you now have two objects, and you need to know which object your references point to. In one case you have a local variable in the other a field. Remember if you don't call the method the field is initialised in, your reference will point to the default value (null) and you know what sort of problems you can get with nulls.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:So Initializing Each time will create any Overload on program efficency or unrfrence Object will be GC(Gargae Collected)?
Yes, but garbage collection is fast and you are unlikely to notice the overhead.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kishor Joshi wrote:So Initializing Each time will create any Overload on program efficency or unrfrence Object will be GC(Gargae Collected)?

Tip for you Kisher:

Don't worry about efficiency ... EVER ... (or at least not until you're much further down the road).

Worry about getting your code right; because that alone will prevent 90% of "inefficiencies" you need to worry about.

And the best way to get code "right" is to keep it simple.

And just in case you need convincing, my favourite quote about "efficiency":
"More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity." — W.A. Wulf

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!