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


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

Re: Java basic question

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.help
Subject Re: Java basic question
Date 2013-08-03 21:51 -0400
Organization A noiseless patient Spider
Message-ID <ktkbn6$dao$1@dont-email.me> (permalink)
References <ktk9ml$5sk$1@dont-email.me>

Show all headers | View raw


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

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