Discussion:
Java basic question
(too old to reply)
isuy
2013-08-04 01:17:21 UTC
Permalink
Suppose I have:

class A {
String index = "string";

String getPage(String str) { return str; }
}




When I call:

new A().getPage("index") , it returns "index" instead of "string".



What Do I need to do to get "string"?

Thank you.
Eric Sosman
2013-08-04 01:51:45 UTC
Permalink
Post by isuy
class A {
String index = "string";
String getPage(String str) { return str; }
}
new A().getPage("index") , it returns "index" instead of "string".
What Do I need to do to get "string"?
There are lots of possibilities, depending on precisely
what you're trying to do. Here are a few:

class A {
String getPage(String ignored) {
return "string";
}
}
// ...
String result = new A().getPage("zaphod");

class A {
String index = "string";
String getPage() {
return index;
}
}
// ...
String result = new A().getPage();

class A {
String index;
A(String thing) {
index = thing;
}
String getPage() {
return index;
}
}
// ...
String result = new A("string").getPage();

class A {
String index = "string";
String getPage(String str) {
if ("index".equals(str)) {
return index;
}
throw new IllegalArgumentException(
"bad argument: " + str);
}
}
// ...
String result = new A().getPage("index");
String failure = new A().getPage("zaphod");

// No "class A" at all, and
String result = "string";

So: What are you trying to do?
--
Eric Sosman
***@comcast-dot-net.invalid
Lew
2013-08-04 03:46:50 UTC
Permalink
Post by Eric Sosman
Post by isuy
class A {
String index = "string";
String getPage(String str) { return str; }
}
You are asking the method to return its argument, 'str', not the member
variable 'index' or anything else.
Post by Eric Sosman
Post by isuy
new A().getPage("index") , it returns "index" instead of "string".
Why do you say "instead of"? You clearly ask 'getPage()' to return its
argument, which in that call evaluates to "index". What else could it do?
Post by Eric Sosman
Post by isuy
What Do I need to do to get "string"?
Code the routine to return what you want instead of its method argument.
Post by Eric Sosman
There are lots of possibilities, depending on precisely
class A {
String getPage(String ignored) {
return "string";
}
}
// ...
String result = new A().getPage("zaphod");
class A {
String index = "string";
String getPage() {
return index;
}
}
// ...
String result = new A().getPage();
class A {
String index;
A(String thing) {
index = thing;
}
String getPage() {
return index;
}
}
// ...
String result = new A("string").getPage();
class A {
String index = "string";
String getPage(String str) {
if ("index".equals(str)) {
return index;
}
throw new IllegalArgumentException(
"bad argument: " + str);
}
}
// ...
String result = new A().getPage("index");
String failure = new A().getPage("zaphod");
// No "class A" at all, and
String result = "string";
So: What are you trying to do?
--
Lew
isuy
2013-08-04 07:54:49 UTC
Permalink
Thank you all for prompt responses.
Very much appreciated.
Post by Lew
Post by Eric Sosman
Post by isuy
class A {
String index = "string";
String getPage(String str) { return str; }
}
You are asking the method to return its argument, 'str', not the member
variable 'index' or anything else.
Post by Eric Sosman
Post by isuy
new A().getPage("index") , it returns "index" instead of "string".
Why do you say "instead of"? You clearly ask 'getPage()' to return its
argument, which in that call evaluates to "index". What else could it do?
Post by Eric Sosman
Post by isuy
What Do I need to do to get "string"?
Code the routine to return what you want instead of its method argument.
Post by Eric Sosman
There are lots of possibilities, depending on precisely
class A {
String getPage(String ignored) {
return "string";
}
}
// ...
String result = new A().getPage("zaphod");
class A {
String index = "string";
String getPage() {
return index;
}
}
// ...
String result = new A().getPage();
class A {
String index;
A(String thing) {
index = thing;
}
String getPage() {
return index;
}
}
// ...
String result = new A("string").getPage();
class A {
String index = "string";
String getPage(String str) {
if ("index".equals(str)) {
return index;
}
throw new IllegalArgumentException(
"bad argument: " + str);
}
}
// ...
String result = new A().getPage("index");
String failure = new A().getPage("zaphod");
// No "class A" at all, and
String result = "string";
So: What are you trying to do?
Loading...