Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. call by value for objects

call by value for objects

Scheduled Pinned Locked Moved Java
questionjava
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mesho
    wrote on last edited by
    #1

    hi all as u know java is using call by reference with objects how can i instead call by value for objects?? thanks in advance

    L A 2 Replies Last reply
    0
    • M mesho

      hi all as u know java is using call by reference with objects how can i instead call by value for objects?? thanks in advance

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, You can't pass an object by value. The best approximation would be to pass a clone of the object. And why would you need to pass by value? :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Merry Christmas and a Happy New Year to all.


      M 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, You can't pass an object by value. The best approximation would be to pass a clone of the object. And why would you need to pass by value? :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Merry Christmas and a Happy New Year to all.


        M Offline
        M Offline
        mesho
        wrote on last edited by
        #3

        i have arraylist of objects and i wrote a method that take one object from the arraylist and return a modified object the problem is after calling the method all objects in the arraylist changed to the new value!!! when i debuge my program i realized that all objects (in the array list and the returning object) have the same id (i'm using eclipse), which is suppose to be unique and because of that any changes to object are applied to all version i don't know way can you help please??:rose:

        L 1 Reply Last reply
        0
        • M mesho

          i have arraylist of objects and i wrote a method that take one object from the arraylist and return a modified object the problem is after calling the method all objects in the arraylist changed to the new value!!! when i debuge my program i realized that all objects (in the array list and the returning object) have the same id (i'm using eclipse), which is suppose to be unique and because of that any changes to object are applied to all version i don't know way can you help please??:rose:

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          from what you told, but did not show, it sounds like you are only creating a single object, and have a loop that (1) modifies it somehow and (2) adds its reference to the arraylist, resulting in a large number of identical references to the single object in its final state. Check: is the "new Whatever(...)" inside or outside your loop? If that does not help, then I suggest you show the relevant code. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Merry Christmas and a Happy New Year to all.


          M 1 Reply Last reply
          0
          • L Luc Pattyn

            from what you told, but did not show, it sounds like you are only creating a single object, and have a loop that (1) modifies it somehow and (2) adds its reference to the arraylist, resulting in a large number of identical references to the single object in its final state. Check: is the "new Whatever(...)" inside or outside your loop? If that does not help, then I suggest you show the relevant code. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Merry Christmas and a Happy New Year to all.


            M Offline
            M Offline
            mesho
            wrote on last edited by
            #5

            my code is very long, that's why i don't write it before i'm writing a program for multiprocessor using genetic algorithm ArrayList POP=new ArrayList (); for(int i=0;i<10;i++) POP.add(generate_schedule()); ArrayList TMP=new ArrayList (); for(int j=0;j < NPOP.size()/2;j++) { schedule s1=NPOP.get(j); schedule s2=NPOP.get(j+(POP.size()/2)); Random rand=new Random(); double r=rand.nextDouble(); if(r<=CrossOver_prob) { ArrayList result=new ArrayList (); result=crossover(s1,s2); TMP.add(result.get(0)); TMP.add(result.get(1)); } else { TMP.add(s1); TMP.add(s2); } } for(int j=0;j < TMP.size();j++) { System.out.println("schedule"+j); Random rand=new Random(); double r=rand.nextDouble(); if(r<=Mutation_prob) **POP.add(Mutation(TMP.get(j)););** else POP.add(TMP.get(j)); } my problem is on the bold line when calling mutation all schedules in TMP is changed hope its clear now :)

            L C 2 Replies Last reply
            0
            • M mesho

              my code is very long, that's why i don't write it before i'm writing a program for multiprocessor using genetic algorithm ArrayList POP=new ArrayList (); for(int i=0;i<10;i++) POP.add(generate_schedule()); ArrayList TMP=new ArrayList (); for(int j=0;j < NPOP.size()/2;j++) { schedule s1=NPOP.get(j); schedule s2=NPOP.get(j+(POP.size()/2)); Random rand=new Random(); double r=rand.nextDouble(); if(r<=CrossOver_prob) { ArrayList result=new ArrayList (); result=crossover(s1,s2); TMP.add(result.get(0)); TMP.add(result.get(1)); } else { TMP.add(s1); TMP.add(s2); } } for(int j=0;j < TMP.size();j++) { System.out.println("schedule"+j); Random rand=new Random(); double r=rand.nextDouble(); if(r<=Mutation_prob) **POP.add(Mutation(TMP.get(j)););** else POP.add(TMP.get(j)); } my problem is on the bold line when calling mutation all schedules in TMP is changed hope its clear now :)

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Sorry, that code is hardly readable; please use PRE tags, not CODE tags, so we get indentation and good contrast. You can still edit the existing message. Furthermore, what needs to be checked is how the values or objects get created that you are adding to the arraylist, hence one has to inspect Mutation(), and whatever is used to create the entries in NPOP (as those will end up in TMP). :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              Merry Christmas and a Happy New Year to all.


              1 Reply Last reply
              0
              • M mesho

                my code is very long, that's why i don't write it before i'm writing a program for multiprocessor using genetic algorithm ArrayList POP=new ArrayList (); for(int i=0;i<10;i++) POP.add(generate_schedule()); ArrayList TMP=new ArrayList (); for(int j=0;j < NPOP.size()/2;j++) { schedule s1=NPOP.get(j); schedule s2=NPOP.get(j+(POP.size()/2)); Random rand=new Random(); double r=rand.nextDouble(); if(r<=CrossOver_prob) { ArrayList result=new ArrayList (); result=crossover(s1,s2); TMP.add(result.get(0)); TMP.add(result.get(1)); } else { TMP.add(s1); TMP.add(s2); } } for(int j=0;j < TMP.size();j++) { System.out.println("schedule"+j); Random rand=new Random(); double r=rand.nextDouble(); if(r<=Mutation_prob) **POP.add(Mutation(TMP.get(j)););** else POP.add(TMP.get(j)); } my problem is on the bold line when calling mutation all schedules in TMP is changed hope its clear now :)

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #7

                Your question is still not very clear because you didn't provide the function prototype (we can only see the content of the function, not the parameters which are passed). Anyway, in Java whenever you manipulate an object, it is similar as using a pointer to the object. So, when you pass the object to a function, you pass the pointer and not a copy of the object. If you really need to make changes in a copy of the object, then you have to create a new instance of the object yourself. There's no such thing as passing by value in java.

                Cédric Moonen Software developer
                Charting control [v2.0] OpenGL game tutorial in C++

                M 1 Reply Last reply
                0
                • C Cedric Moonen

                  Your question is still not very clear because you didn't provide the function prototype (we can only see the content of the function, not the parameters which are passed). Anyway, in Java whenever you manipulate an object, it is similar as using a pointer to the object. So, when you pass the object to a function, you pass the pointer and not a copy of the object. If you really need to make changes in a copy of the object, then you have to create a new instance of the object yourself. There's no such thing as passing by value in java.

                  Cédric Moonen Software developer
                  Charting control [v2.0] OpenGL game tutorial in C++

                  M Offline
                  M Offline
                  mesho
                  wrote on last edited by
                  #8

                  Luc Pattyn Cédric Moonen thank u all for your reply its clear now that's my problem was i don't create a new insantce of my objects but i referenced to existing object schedule s = POP.get(i); where POP is arraylist of schedules again thank u for ur help:rose::rose::rose:

                  1 Reply Last reply
                  0
                  • M mesho

                    hi all as u know java is using call by reference with objects how can i instead call by value for objects?? thanks in advance

                    A Offline
                    A Offline
                    April Fans
                    wrote on last edited by
                    #9

                    1.in the same class, you can not directly create objects 2.in other classes, you must create an object to call by value for objects 3.the class can be defined as static ,or you can directly call the class' name

                    April Comm100 - Leading Live Chat Software Provider

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups