Results 1 to 10 of 10
  1. #1
    zelenkooo is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Basic java reference problem

    I need to send some data from method but without return

    class 1

    Obj1 obj=new Obj1();
    int i=Class2.DoSomething(obj);
    ...
    do something with obj


    class 2
    public static int DoSomething(Obj1 obj) {
    .....
    obj = (Obj1) something.something();
    return 5;
    }

    Here is the problem, from class 1 i call method of class 2 and am setting obj there but when i use it latter in class 1 it's not set :(

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Basic java reference problem

    The obj variable in the argument list for the DoSomething method is local to that method. When the method exits, the variable is discarded. Any value you give it will be lost.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default Re: Basic java reference problem

    The parameter 'obj' in that static method is a reference by itself; changing it in that method doesn't change any other reference outside of that method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    zelenkooo is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Basic java reference problem

    Yes i see the problem but i need the solution for this

    1. cant use return
    2. have to change object and use it in other class
    Last edited by zelenkooo; 12-05-2011 at 02:22 PM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Basic java reference problem

    What do you want the DoSomething method to do?

  6. #6
    zelenkooo is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Basic java reference problem

    method is using sockets and connecting to server , retrieving object that i cast

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Basic java reference problem

    What does that have to do with the sample code you posted?
    It shows the DoSomething method returning a int value: 5
    What more do you want it to do?

  8. #8
    zelenkooo is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Basic java reference problem

    i fixed it with this

    class 1

    Obj1 obj=new Obj1();
    int i=Class2.DoSomething(obj);
    ...
    do something with obj


    class 2
    public static int DoSomething(Obj1 obj) {
    .....
    obj2 = (Obj1) something.something();
    obj.setAllValues(obj2); // set all values as in obj2
    return 5;
    }

    there could be a better solution but this works

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,611
    Rep Power
    25

    Default Re: Basic java reference problem

    I'm not sure I follow your example. For example what is the something variable? What does the something() method return?
    If you call methods in a class, those methods should be able to change the contents of that object.

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default Re: Basic java reference problem

    That seems (on the face of it) a rather odd way of going about things...

Similar Threads

  1. Basic Java Calculator Code Problem
    By SerbianSergeant in forum New To Java
    Replies: 7
    Last Post: 07-22-2011, 02:50 PM
  2. Very basic java problem
    By mrparker2 in forum New To Java
    Replies: 6
    Last Post: 05-26-2011, 09:50 PM
  3. Basic Java GUI problem, JPanel not resizing.
    By Cemi in forum New To Java
    Replies: 10
    Last Post: 04-11-2010, 02:20 AM
  4. Triangles java problem (basic help)
    By adz06 in forum New To Java
    Replies: 5
    Last Post: 10-31-2009, 07:58 PM
  5. [SOLVED] very basic java problem
    By sales1 in forum New To Java
    Replies: 13
    Last Post: 08-20-2008, 09:33 PM

Posting Permissions

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