Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #2746 > unrolled thread
| Started by | isuy <isuy@socal.rr.com> |
|---|---|
| First post | 2013-08-03 18:17 -0700 |
| Last post | 2013-08-04 00:54 -0700 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.java.help
Java basic question isuy <isuy@socal.rr.com> - 2013-08-03 18:17 -0700
Re: Java basic question Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-03 21:51 -0400
Re: Java basic question Lew <lewbloch@gmail.com> - 2013-08-03 20:46 -0700
Re: Java basic question isuy <isuy@socal.rr.com> - 2013-08-04 00:54 -0700
| From | isuy <isuy@socal.rr.com> |
|---|---|
| Date | 2013-08-03 18:17 -0700 |
| Subject | Java basic question |
| Message-ID | <ktk9ml$5sk$1@dont-email.me> |
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.
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2013-08-03 21:51 -0400 |
| Message-ID | <ktkbn6$dao$1@dont-email.me> |
| In reply to | #2746 |
On 8/3/2013 9:17 PM, isuy wrote:
> 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"?
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
esosman@comcast-dot-net.invalid
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2013-08-03 20:46 -0700 |
| Message-ID | <77e17aa4-5636-4f6a-b496-c8a3dfac9ade@googlegroups.com> |
| In reply to | #2747 |
Eric Sosman wrote:
> isuy wrote:
>> Suppose I have:
>>
>> 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.
>>
>> When I call:
>>
>> 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?
>> What Do I need to do to get "string"?
Code the routine to return what you want instead of its method argument.
> 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?
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | isuy <isuy@socal.rr.com> |
|---|---|
| Date | 2013-08-04 00:54 -0700 |
| Message-ID | <ktl0vs$qd9$1@dont-email.me> |
| In reply to | #2748 |
Thank you all for prompt responses.
Very much appreciated.
On 08/03/2013 08:46 PM, Lew wrote:
> Eric Sosman wrote:
>> isuy wrote:
>>> Suppose I have:
>>>
>>> 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.
>
>>>
>>> When I call:
>>>
>>> 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?
>
>>> What Do I need to do to get "string"?
>
> Code the routine to return what you want instead of its method argument.
>
>> 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?
>
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.help
csiph-web