File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Initializations and declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initializations and declarations" Watch "Initializations and declarations" New topic
Author

Initializations and declarations

Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
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?


May the force of the Java be in all of us !!!
Arun Boraiah
Ranch Hand

Joined: Nov 28, 2001
Posts: 233
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


Sharing is learning
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
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

Joined: Jul 11, 2001
Posts: 14112

This type of initialization obviously can only be done in the constructor...
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
Ilja,
Thanks, I get it now about the constructors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initializations and declarations