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 STILL WAITING FOR A SATISFACTORY REPLY!!! :( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "STILL WAITING FOR A SATISFACTORY REPLY!!! :(" Watch "STILL WAITING FOR A SATISFACTORY REPLY!!! :(" New topic
Author

STILL WAITING FOR A SATISFACTORY REPLY!!! :(

ankush walia
Ranch Hand

Joined: Jan 24, 2001
Posts: 95

hi everyone;

class abcd{
int a;
abcd(int x) {}
//constructor with nothing inside made just to
//destroy or u can say nullify the default constructor
//OVERLOADING THE CONSTRUCTOR
public static void main(String s[])
{ abcd obj = new abcd(5);
System.out.println(obj.a);
System.out.println(s);
}
}
# How does a get intialized even when the default constructor
has been overloaded??And if it is not the task of constructor
to initialize the class variables then who does this job??
# 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!!
ANKUSH!!
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

If it is not the task of constructor
to initialize the class variables then who does this job??
It is the job of the JVM(java virtual machine).
System.out.println(s);
This prints out the memory location where the String[] is stored.


SCJP
Visit my download page
niraj singh
Ranch Hand

Joined: Feb 07, 2001
Posts: 36
Hi Ankush,
A constructor can be used in the initialization of fields.
If you run a program, initialization of fields occur in a particular sequence -
1. first all static fields and blocks are initialized in the order they appear in the code (code is interpreted).
2. then the main() method of the class is run.
As you notice, i have not mentioned the constructor. In fact, the constructor does not get called at all. Example -
class abc {
static int i = 10;
int j;
abc() {
System.out.println("I am in the constructor");
i = 100;
j = 2;
System.out.println("j ==" + j);
}
public static void main {
System.out.println(i);
}
}
If you run this code, you will see the output is 10, not 100 as set in the constructor. The constructor is not being run at all, (nor is the int j instance field being initialized) as per the initialization sequence I mentioned in the beginning. If you want the constructor to be called, you have to explicitly call the constructor with a - new abc() statement. If you insert this statement -
abc ob = new abc();
as the first line of the main() method, then it will call the constructor and set your field i as 100. Thereafter, the output of i will be 100.
So, what i am trying to say is that Java always initializes a field (depending on it's type) to its default value, the moment
it accesses it the first time. If you declare and initialize the field as -
static int i = 10; instead of just declaring it as I have done above, even then, Java will first initialize int i as 0 and then initialize i to the value you have initialized it to.
Hope this makes the first part of your question clear.
The answer to the second part, is that the output you are getting is a reference to the location of the String object in memory. You can also see this value by using -
args.getClass().getName();
this will return [L
the rest part @.... is the reference to the memory location
See Class.getName in your Java documentation for details.
Stephen Joseph
Ranch Hand

Joined: Dec 29, 2000
Posts: 50
class abcd{
int a;
abcd(int x) {}
//constructor with nothing inside made just to
//destroy or u can say nullify the default constructor
//OVERLOADING THE CONSTRUCTOR
public static void main(String s[])
{ abcd obj = new abcd(5);
System.out.println(obj.a);
System.out.println(s);
}
}
# How does a get intialized even when the default constructor
has been overloaded??And if it is not the task of constructor
to initialize the class variables then who does this job??
# 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 "

The answer of your first question I can only give by seeing your output,because I couldnt get your q
Anyway the second q's answer is that it is interpreted as
Name of the class(represented by the object)@ hashcode in hexadecimal form
Here it is the object of a string array,it is converting the object(of string array)into string by toString() method,
In order to recieve the output you want, you have to write
for(int i=0;i<s.length;i++)>
{
System.out.println(s[i]);
}
Regarding your first question I can say that because the variables are instance variables they don't need to be initialized they are initialized when their constructors are instantiated.
Hope you get it
Thanks
Steven

Stephen Joseph
Ranch Hand

Joined: Dec 29, 2000
Posts: 50
there was a omission
for(int i =0; i<s.length;i++)>
{
System.out.println(s[i]);
}
This will give u all the arguments on cammand line in the form of an array
Thanks
Steven
Stephen Joseph
Ranch Hand

Joined: Dec 29, 2000
Posts: 50
there was a omission
for(int i =0; i.lt.s.length;i++)
{
System.out.println(s[i]);
}
This will give u all the arguments on cammand line in the form of an array
Thanks
Steven
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: STILL WAITING FOR A SATISFACTORY REPLY!!! :(