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


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

Re: Java basic question

Newsgroups comp.lang.java.help
Date 2013-08-03 20:46 -0700
References <ktk9ml$5sk$1@dont-email.me> <ktkbn6$dao$1@dont-email.me>
Message-ID <77e17aa4-5636-4f6a-b496-c8a3dfac9ade@googlegroups.com> (permalink)
Subject Re: Java basic question
From Lew <lewbloch@gmail.com>

Show all headers | 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