This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Simple constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Simple constructor" Watch "Simple constructor" New topic
Author

Simple constructor

Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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

Joined: Nov 24, 2000
Posts: 776
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

Joined: Jul 29, 2001
Posts: 78
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

Joined: Jul 20, 2001
Posts: 464
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

Joined: Aug 30, 2000
Posts: 3141
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).]


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Simple constructor
 
Similar Threads
static Again...
How do you solve this...??!!
simple program output?
Constructors
difference between String and StringBuffer classes