Just want to confirm if it is possible to set in this case (Example 1) class Two's pumpId variable by sending it as a parameter to class One's setPumpId method without using the usual return method assignment like :

pumpId = o.getPumpNum()

I think in C++ we can do that, but just ain't too sure with JAVA and I'm having problem returning more than one value from a method with just a return statement in the end which I think is impossible. :-|


Example 1:

public Class One {

   private int pumpNum = 3;

   public void setPumpId(int pumpId)
   {
       pumpId = pumpNum; 
   }

}

public Class Two { 

   private int pumpId = 0;
   private One o = new One();
   
   o.setPumpId(pumpId); 

}

Thanks in advance for any replies. Really need this for my petrol station simulation project. :mrgreen:

Why not just put the set method in class 2? class 1 wont be able to see the variable anyway.

Why not just put the set method in class 2? class 1 wont be able to see the variable anyway.

Well, I am looking for a way to pass a variable from one class as a parameter, to another class's method and set it there, without using a return statement from the other class's method.

Or is there any way to return more than one value from a method.

Hmm....maybe the scenario i put up earlier is unclear and ambiguous. Sorry :sad:

It has to be between two classes though. MultiThreading's kinda tough with JAVA :mad: , put 4 petrol pumps and 20 customers and the system's crawling like a snail...:sad: :cry: :-| :eek: ...wish I could do it in Delphi...:twisted:

Thanks for the reply anyway. ;)

Ok, I think I see what you're saying. Objects are passed by reference. So as long as you're not trying to do this with any primitives (int, float) then anything you do the passed variable has immediate affect on it.

Ok, I think I see what you're saying. Objects are passed by reference. So as long as you're not trying to do this with any primitives (int, float) then anything you do the passed variable has immediate affect on it.

Yeah just found out this from the web:

O'Reilly's Java in a Nutshell by David Flanagan puts it best: "Java manipulates objects 'by reference,' but it passes object references to methods 'by value.'"

Forgot that JAVA behaves like that cause there are no * and & to use.....still in the C++ mindset....hehe :cheesy:

Guess I have to pass the object to change the value of the class's variable instead of passing just the variable.:idea:

Thanks a lot! This is refreshing....;):o

Ok, I think I see what you're saying. Objects are passed by reference. So as long as you're not trying to do this with any primitives (int, float) then anything you do the passed variable has immediate affect on it.

WRONG WRONG WRONG!

Objects are NEVER passed in Java AT ALL.
Neither is anything ever passed by reference.

Instead references are passed by value.
You can NOT change the reference and expect the one in the calling method to change.
You can however change datamembers on the reference and those changes will be reflected because they're actual changes on the reference that's residing inside the calling method.

Instead references are passed by value.

how do you pass a reference by value? that doesn't even make sense. unless you saying the value of the memory address to the reference?
Maybe I'm misunderstanding what you're trying to say, are you saying this wouldn't show affect on the object outside the method?

public static void main(String[] args)
    {       
        Thing thing = new Thing(15);
        System.out.println(thing.getX());
        resetX(thing,42);
        System.out.println(thing.getX());
    }
    
    public static void resetX(Thing t, int x)
    {
        t.setX(x);
    }

no, you misunderstand.

the reference is passed by value. That means you cannot change the reference itself, but you can change the content of the thing you're referencing.
So in your case the object referenced by t gets its data changed.
But the following would not work:

public static void resetX(Thing t, int x)
    {
        t = new Thing(x);
    }

The Thing outside the method would not get replaced by the new one, which goes out of scope on leaving the method and is lost forever.
Were Java using pass by reference (which it doesn't), the object outside the method would get replaced by the new one because the reference to it would be replaced by a new one.
Since Java uses pass by value, you're actually passing a copy of the reference to the method, so another reference pointing to the same memory space (thus Object).

It's a trap many beginners fall into, especially those coming from a C or C++ background where pass by reference (or passing pointers) is the standard way and such things are possible).

Just want to confirm if it is possible to set in this case (Example 1) class Two's pumpId variable by sending it as a parameter to class One's setPumpId method without using the usual return method assignment like :

pumpId = o.getPumpNum()

I think in C++ we can do that, but just ain't too sure with JAVA and I'm having problem returning more than one value from a method with just a return statement in the end which I think is impossible. :-|


Example 1:

public Class One {

   private int pumpNum = 3;

   public void setPumpId(int pumpId)
   {
       pumpId = pumpNum; 
   }

}

public Class Two { 

   private int pumpId = 0;
   private One o = new One();
   
   o.setPumpId(pumpId); 

}

Thanks in advance for any replies. Really need this for my petrol station simulation project. :mrgreen:

hi mate
do read on the concept of freind functions
it is a straight forward thing using friend functions
as regards to u r inability to return mulitples values in a function
iam sorry c, c++ and java dont suppor them altough
c# supports this feature :)

hi mate
do read on the concept of freind functions
it is a straight forward thing using friend functions
as regards to u r inability to return mulitples values in a function
iam sorry c, c++ and java dont suppor them altough
c# supports this feature :)

Hmm...this topic was months ago though....and it was not about returning multiple values....

Errr...do know bout the friend function thing, but it's not a solution to the problem I had in "Java".... and it's not really a good solution when it's like... you can do this and that with this language but too bad the language you're using does not support this? :-|

anyway "C++" do have friend functions and class..... it's not a new thing that came up with "C#" :rolleyes:

Just want to confirm if it is possible to set in this case (Example 1) class Two's pumpId variable by sending it as a parameter to class One's setPumpId method without using the usual return method assignment like :

pumpId = o.getPumpNum()

I think in C++ we can do that, but just ain't too sure with JAVA and I'm having problem returning more than one value from a method with just a return statement in the end which I think is impossible. :-|


Example 1:

public Class One {
 
   private int pumpNum = 3;
 
   public void setPumpId(int pumpId)
   {
       pumpId = pumpNum; 
   }
 
}
 
public Class Two { 
 
   private int pumpId = 0;
   private One o = new One();
 
   o.setPumpId(pumpId); 
 
}

Thanks in advance for any replies. Really need this for my petrol station simulation project. :mrgreen:

I know it's mad late, but were you looking for something unconventional like the following:

public Class One {
 
  protected int pumpNum = 3;//any class within the same package 
                                          //can manipulate this variable.
//This class might as well be static and pumpNum made a java 
//constant
}
 
public Class Two { 
  private One o;
  private int pumpId;
 
  public Two() {
     o = new One();
  }
 
  public void setPumpID() {
     pumpId = o.pumpNum;
 }
}

What I meant was instead of using let's say...

public class One {
   private int pumpNum = 3; 
   
   public int getPumpNum()
  {
      return pumpNum; 
   }
 
}

public class Two { 
   public static void main(String[] args)
   {
       int pumpId = 0; 
       One o = new One(); 
       pumpId = o.getPumpNum(); 
    }
}

to set the value for class Two's pumpId variable....

Are there any ways where instead... I pass in the Two's pumpId into a method in class One and change the value.. without using any assignment statement, which can be done in C++ but was wondering if it's possible in Java.

The class One cannot be static and the variable cannot be constant... cause it too changes as the threads in the program runs. A bit tricky in a way.

The only way to solve it at that point was to find a way to pass the variable by reference... which in Java is quite impossible to do with primitive types. Which is quite confusing for someone who came from a C background.... just like things such as Generics in Java would most likely be mistaken by a C programmer as a Template-like thing.

The real program was really complicated and involved a lot of threads.... anyway.. I did the logic in a completely different way producing the results that I wanted on approximately the 24th of May.

Anyway.... thanks a lot to everyone for replying and I should probably notify anyone who's reading this RIGHT NOW, that the problem has already been solved. And if anyone knows how to close a thread in this forum... please notify me... I don't want to waste anyone's time on something that's been solved where they can use it to help other people in here.;)

Hi guy or gal!!! I know you have solved this already but it is an interesting issue. I was wondering if you used wrapper classes to solve it. I'm pretty much new to programmer and I like learning from interesting scenarios. Although it has been solved, it does make interesting conversation. Check out the following code, was yours similiar?

public class One {
   private int pumpNum = 3; 
 public one() { }
 
   public void setPumpNum(int pumpNumber) { 
    pumpNum = pumpNumber; //sets the pump number
 } 
   public void getPumpNum(Integer pumpId) { 
    pumpId = (Integer) pumpNum; //wraps a primitive int in the
                                                 //wrapper object of class Two
 } 
 
}
 
public class Two { 
   private Integer pumpId = null;
   private One o = null;
 
 public static void main(String[] args) {
      o = new One(); 
      o.getPumpNum(pumpId);//pumpId will have to be unwrapped to
                                        //use the int value
 }
}

What I meant was instead of using let's say...

public class One {
   private int pumpNum = 3; 
 
   public int getPumpNum()
  {
      return pumpNum; 
   }
 
}
 
public class Two { 
   public static void main(String[] args)
   {
       int pumpId = 0; 
       One o = new One(); 
       pumpId = o.getPumpNum(); 
    }
}

to set the value for class Two's pumpId variable....

Are there any ways where instead... I pass in the Two's pumpId into a method in class One and change the value.. without using any assignment statement, which can be done in C++ but was wondering if it's possible in Java.

The class One cannot be static and the variable cannot be constant... cause it too changes as the threads in the program runs. A bit tricky in a way.

The only way to solve it at that point was to find a way to pass the variable by reference... which in Java is quite impossible to do with primitive types. Which is quite confusing for someone who came from a C background.... just like things such as Generics in Java would most likely be mistaken by a C programmer as a Template-like thing.

The real program was really complicated and involved a lot of threads.... anyway.. I did the logic in a completely different way producing the results that I wanted on approximately the 24th of May.

Anyway.... thanks a lot to everyone for replying and I should probably notify anyone who's reading this RIGHT NOW, that the problem has already been solved. And if anyone knows how to close a thread in this forum... please notify me... I don't want to waste anyone's time on something that's been solved where they can use it to help other people in here.;)

Well first of all I'm a guy... see the pic up there? It's me. Chinese dude wanna be programmer.

Second... I've solved the problem that I had with a completely different logic, which is a totally different topic than the one posted here.

I think I should give you an idea what I was trying to achieve during this posts by giving a simple C++ example here.

int main()
{
    int number = 5;
    
    cout << "The original value : " << number; // number value is 5
    
    changeValue(&number);

    cout << "The new value : " << number; // number value is 10
}

void changeValue(int *nPtr)
{
    *nPtr = 10; 
}

As you can see from this code example, I'm only passing in the variable to a method that does not return (no return statement) any values to change the number value in main. And no assignment operator '=' in the main has been used. And I did not want a copy of the number variable to be passed into the method (passing by reference).

Java:

public class One { 
    private int pumpNum;
    public One()
    {
        pumpNum = 0;
    }
    
    public void passPumpNum(int p)
    {
        p = pumpNum; 
    }
}

public class Two { 
    public static void main(String[] args)
    {
        int x = 0; 
        System.out.println("\nOld value : " + x);
        One o = new One();
        o.passPumpNum(x);
        System.out.println("\nNew value : " + x);
    }
}

init:
deps-jar:
compile:
run:

[B]Old value : 0

New value : 0[/B] <-----Nothing changed because of passing by value
BUILD SUCCESSFUL (total time: 0 seconds)

But I can't do this in Java. That is why I solved it in a totally different way and is totally not related to this topic. Even if it's possible, I doubt that it is a recommended practice in Java. Hope you understand what I'm trying to say here :confused:

Your code when corrected:

public class One { 
     private int pumpNum = 3;  
     public One() { } // typo error here, yours was one
     public void setPumpNum(int pumpNumber) 
     {  pumpNum = pumpNumber; //sets the pump number  
     } 
     public void getPumpNum(Integer pumpId) 
     {   pumpId = (Integer) pumpNum;
     }  
 }

public class Two {
    private static Integer pumpId = null; // must be declared static cause              
                                                         // your main is static and you   
                                                  // can't reference a non static variable
    private static One o = null;   
    public static void main(String[] args) 
    { 
        o = new One(); 
        o.getPumpNum(pumpId);
        
        if(pumpId != null) // if there is a value after the passing
            System.out.println("pumpId value = " + pumpId);
    }
}

Output: 
init:
deps-jar:
compile:
run:
BUILD SUCCESSFUL (total time: 0 seconds)

Same goes with yours... only the copy of the variable gets modified and not the original. No output in this case.

That is why I've stopped... and started to solve my problem in a totally different way for the current one is not feasible to dwelve into deeper due to a very scary time constraint. My Java sucks anyway hehe...:mrgreen: master it in a week? No way :eek:

My Java sucks anyway hehe... master it in a week? No way

That's the path to enlightenment. I've been doing Java since 1997 (and professionally since 1999) and there are a lot of things I don't know about it :)

But knowing that you don't know is the first step towards knowing more, because you can't start to learn something before you know it's there to be learned.

This article has been dead for over six months. Start a new discussion instead.