wood burning stoves
The moose likes Beginning Java and the fly likes INTERESTING QUESTIONS!! @@ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java


ankush walia
Ranch Hand

Joined: Jan 24, 2001
Posts: 95
class abcd{
int a;
abcd(int x) {}
//constructor with nothing inside made just to
//destroy or u can say nullify the default constructor
public static void main(String s[])
{ abcd obj = new abcd(5);
Now i have two questions to ask over here--->??
# It is very well known that it is the constructor that is
used for initializing the variables!!If this is true then
how is it that my variable "a" gets initialized,even though
i have overloaded the default constructor and in the new
constructor i'm doing nothing?? Is it done by JRE,but how
does it do that exactly when i have overloaded the
# In this program i'm simply running the code without passing
any command line parameters(i.e. simply "java abcd")!!then
when i print the "String s",i get the following address as
output---> " [Ljava.lang.String;@f5bOc86d "

What does this address mean and where does it come from??
Plzzzzzzzz do reply back soon!!
shilpa reddy

Joined: Dec 12, 2000
Posts: 29
I can take a shot here.
1) a is class variable. Class variables are automatically intialized during object creation if they have not been explicitly initialized
An int is initialized to 0 by default. That is why you should get a 0 when you print the value for a.This has nothing do with your constructor,since you are not initializing a in your constructor
2) The second println should have given you the memory address of the String object that you declared in your main signature.
Hope this helps.
Asuthosh Borikar
Ranch Hand

Joined: Sep 29, 2000
Posts: 75
The answer to your first question is that it is N O T the constructor that is used to initialize the member variables of a class. All the member variables in a class are assigned default values(int variables 0; Objects null; boolean false etc) even before executing the code in the constructor. Constructors need only to be used if you the values you want to assign to the member variables are different from their respective default values.
I do not have a satisfactory explanation to your second question.
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
The variables defined in a class are actually initialize at the time when the constructor is called. It doesn't mean that you have to initialize those variables in the constructor itself.

Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
Hema Sukumar

Joined: Dec 22, 2000
Posts: 22
Doesn't int a, has to be static to be called as a member/class variable ?
Hema Sukumar

Joined: Dec 22, 2000
Posts: 22
Sorry, I meant class Variable.
What I meant was, int a is not a class variable but a member/instance variable.
I agree. Here's the link: http://aspose.com/file-tools
It's not a secret anymore!