Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.help > #2748

Re: Java basic question

X-Received by 10.224.103.68 with SMTP id j4mr18721949qao.8.1375588010703; Sat, 03 Aug 2013 20:46:50 -0700 (PDT)
X-Received by 10.50.120.8 with SMTP id ky8mr183791igb.11.1375588010552; Sat, 03 Aug 2013 20:46:50 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!f7no648096qan.0!news-out.google.com!a13ni157qay.0!nntp.google.com!fx3no689928qab.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.java.help
Date Sat, 3 Aug 2013 20:46:50 -0700 (PDT)
In-Reply-To <ktkbn6$dao$1@dont-email.me>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
NNTP-Posting-Host 173.164.137.214
References <ktk9ml$5sk$1@dont-email.me> <ktkbn6$dao$1@dont-email.me>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <77e17aa4-5636-4f6a-b496-c8a3dfac9ade@googlegroups.com> (permalink)
Subject Re: Java basic question
From Lew <lewbloch@gmail.com>
Injection-Date Sun, 04 Aug 2013 03:46:50 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.java.help:2748

Show key headers only | View raw


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

Back to comp.lang.java.help | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web