File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple constructor

 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I just have this simple question
public class Bll {
String s ;
int a;
Bll(){
this (100, "Ragu");
}
Bll(int a, String s) {
a = a;
s = s;
}
public static void main (String args[]) {
Bll b = new Bll();
System.out.println(b.s);
}
}
The output returns null
If i change it to
this.s=s;
this.a=a;
The output is Ragu
Though i understand the reason for the outputs, i guess
what i am wondering is what exactly is happening
when its declared
a=a;
s=s;
inside the constructor?
Based on the output the basically the assignment havent even
taken place?
Gurus, please explain...
Thankx
 
Guy Allard
Ranch Hand
Posts: 776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, assignments have taken place. But you are just assigning a parameter variable to itself.
The fact that you have a parameter variable (reference) with the same name as a instance variable hides the instance variable.
So:
a=a; // assign inbound a to inbound a
this.a=a; // assign instance variable to inbound
HTH, Guy
 
chao-long liao
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excuse me,I don't understand about the code.
There is no implicit call (this) at the line a=a;why??
What situiation will call implicit (this)??
----------------------------------
thanks for help,Liao
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Guy Allard:
Well, assignments have taken place. But you are just assigning a parameter variable to itself.
The fact that you have a parameter variable (reference) with the [b]same
name as a instance variable hides the instance variable.
So:
a=a; // assign inbound a to inbound a
this.a=a; // assign instance variable to inbound
HTH, Guy[/B]

Well, "this = a" assign instance variable to bound, ok thats fine
but, if "a=a" is assigning inbound then when i print out a or s
how come iam getting 0 or null?

 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ragu,
<code>0</code> is the default value for an <code>int</code> and <code>null</code> is the default for a <code>String</code>. These are assigned to the instance fields <code>int a</code> and <code>String s</code> when you create the new <code>Bll()</code> object.
As Guy pointed out, the parameters you are using in the second constructor are hiding the instance variables; it is not assigning any values to them. Therefore, when you call <code>System.out.println(b.s)</code>, <code>s</code> still has it's default value <code>null</code> and that's what you see.
For more information on hiding see JLS §8.3.3.1
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited August 12, 2001).]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic