Results 1 to 5 of 5
  1. #1
    limp is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Unhappy Copy string to string.

    Hi to all,

    I just have a very begginers question and I was wondering if you can help me.

    Let's say that I am having a variable a in a class b and a variable c in the class that I am working now. Both of the variables are strings. How can I copy the contents of variable c to variable a?

    I tried something like

    b.a.valueOf(c.getText().trim()); but it gave me the following error:

    The static method valueOf(Object) from the type String should be accessed in a static way

    Thank you very much for your time.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'd use public getters and setters to get c's value and set a's value.

    also, look up the String API for the the valueOf(...) method. It is called String.valueOf(some primitive here) and returns the primitive's value as a String. You shouldn't call it "on a String" variable as that would be meaningless.

  3. #3
    limp is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

    Default

    Hi!

    Thank's a lot for the reply. I managed to make it work by using getters and setters however, I was wondering if there is an easier way for doing that. In general, to take a vlaue of a string, , where 'a' is a String object and assign it to different string object 'b' by doing b=valueof(a), should be enough, shouldn't be?


    Thanks a lot!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To directly assign the value of String a to b, all you need to do is this:

    Java Code:
    b = a:
    The getters / setters have little to do with this but instead is to help your program be a well-behaving object oriented program.
    Last edited by Fubarable; 03-17-2009 at 05:55 PM.

  5. #5
    limp is offline Member
    Join Date
    Mar 2009
    Posts
    3
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2014, 08:06 AM
  2. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  3. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  4. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •