Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializations and declarations

 
Shashank Gokhale
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
What is the benefit of initializing a memeber variable in a constructor of that class instead of at the point of declaration?
Also, is there a difference in intializing a string with a literal as opposed to calling its constructor with a string argument?
See my code:
class confoosed
{
String wmw;
String sd="same difference";
String uta=new String("Upping the ante");
confoosed()
{
wmw="What, me worry?";
}
}
Also, what are the different ways to inditialize an array once its declared?
The ways I know are
int i[][]=new int[4][];
int[1]=new int[5];
int [1,1]=54;
int i[][]={{1,2,3},{3,4,6,5},{4,3,5,4,5},{1,2}};
is
int i[][]=new int i[][] {{1,3},{2,4,5}};
also a legal way?
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All member variables get initialized to default value even if not specified. I feel initializing through constructor will be better because on overloading the constructor we can change the initialized values for a given run. But again it depends on the code you are writing.
-arun
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shashank Gokhale:
What is the benefit of initializing a memeber variable in a constructor of that class instead of at the point of declaration?

At the point of declaration, the value might not be known - for example if it is calculated from a constructor argument.

Also, is there a difference in intializing a string with a literal as opposed to calling its constructor with a string argument?

Yes - a String literal is put into the String constant pool, whereas a String constructed by calling the constructor will put a copy of the given literal on the heap. There is no important difference in observable behaviour, though - the latter is just a more wastefull way of accomplishing the same as the former...
 
Shashank Gokhale
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja,
I think I understand what you mean about the creation of a String with the new operator vs with a literal string.
The 'new' operator
String alpha=new String ("Down with duds");
creates a new object on the heap, whereas the other way,
String alpha="Down with duds";
a new reference is created to that literal.
Am I right in understanding this?
I did not understand what you meant by
At the point of declaration, the value might not be known - for example if it is calculated from a constructor argument.

in response to
What is the benefit of initializing a memeber variable in a constructor of that class instead of at the point of declaration?

Can you give an example please?
Also what are the different ways to initialize an array?
[ January 08, 2003: Message edited by: Shashank Gokhale ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This type of initialization obviously can only be done in the constructor...
 
Shashank Gokhale
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja,
Thanks, I get it now about the constructors
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic