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 variable initialisation 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 "variable initialisation" Watch "variable initialisation" New topic
Author

variable initialisation

weiliu lili
Ranch Hand

Joined: Apr 11, 2002
Posts: 46
public class Test026
{
static Test026 t = new Test026();
String str;
public static void main(String args[]) {
Test026 t = new Test026();
t.method("0");
}
void method(String str) {
str = str;
System.out.println(str);
System.out.println(t.str);
System.out.println(this.str);
}
}
-----------------------------------------------------------------------
The output is:
0
null
null

the explanation:
The str for object t and this remains uninitialised and keep null value
I thought str has already been initialised as "str=str", where m i wrong?
thanks
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi Weilliu
change
str = str;
to
this.str = str;
str is an object given as a parameter. For accessing class's str you must refer to this
Thanx,
Jamal Hasanov
www.j-think.com
Karen Leoh
Ranch Hand

Joined: Dec 03, 2001
Posts: 40
Hi there..
I hope this helps. When you initialise "str=str", it is done locally. The instance variable is str is still null.
Correct me if I'm wrong.
public class Test026
{
static Test026 t = new Test026();
String str; //this is initialised to null
public static void main(String args[]) {
Test026 t = new Test026();
t.method("0");
}
void method(String str) {
str = str;
System.out.println(str);
System.out.println(t.str); //this called the instance variable str which is null
System.out.println(this.str); //this also call the instance variable str
}
}


--------------------<p>Karen Leoh<br />Sun Certified Programmer for Java™ 2 Platform
Kaspar Dahlqvist
Ranch Hand

Joined: Jun 18, 2001
Posts: 128
Howdy!
Method void method(String s) is currently too ambiguous for the compiler... Right now your method receives a reference to a String called str, and what you do with
str = str;
is to tell Java that you want str to reference the String that str points to. But, hey, it already does...
In your code you create two objects from the class Test026, and each has a instance variable called str. To initialize these to values other than null, you need to use the references to those objects. In other words, in an appropriate place, do the following:
t.str = str;
this.str = str;

Getting the hang of riding, yet?
/Kaspar
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variable initialisation
 
Similar Threads
mock exam Q
mock exam
plz explain this code
static object initialization
references and method parameters