• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

variable initialisation

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
}

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic