954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

calling back a variable from void method

How do i call back a variable in void method into the main method??

public void multiply (int x)
	{
		Vector3 v3 = new Vector3(num1*x,num2*x,num3*x);
	}
Gsterminator
Light Poster
31 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

What do you mean by "call back"? The simplest and the logical way would be to change the return type of your method to "Vector". Some other approaches would be to pass in a reference to Vector and update the Vector in your "multiply" method or have a static member in your main class but then again not recommended.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

i'm trying to change the components in variable v3.I already have different components in the main but i want to change v3 from my "class Vector3"/void multiply method this is what i have:

public static void main ( String [] args ) throws Exception
   {
      Vector3 v1 = new Vector3(1,2,3);
      System.out.println("v1 = " + v1);
      Vector3 v2 = new Vector3(2,7,1);
      System.out.println("v2 = " + v2);
      Vector3 v3 = v1.add(v2);
      System.out.println("v3 = " + v3);
      v3.multiply(5);
      System.out.println("v3 = " + v3);
Gsterminator
Light Poster
31 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Create the Vector as Instance or Class Variable.

vealparry
Light Poster
27 posts since Jan 2011
Reputation Points: 10
Solved Threads: 5
 

i did create a class variable: class Vector3....and here's the code:

class Vector3
{
	public Vector3 v3;
	private int num1;
	private int num2;
	private int num3;
//------------------------------------------------------------------------
	public Vector3( int num1, int num2, int num3)
	{
		this.num1 = num1;
		this.num2 = num2;
		this.num3 = num3;
	}
//------------------------------------------------------------------------
	public String toString()
	{
		return "(" + num1 + "," + num2 + "," + num3 + ")";
	}
//------------------------------------------------------------------------
	public Vector3 add(Vector3 t)
	{
		return new Vector3(num1 + t.num1 , num2 + t.num2 , num3 + t.num3);
	}
//------------------------------------------------------------------------
	public void multiply (int x)
	{
		Vector3 v3 = new Vector3(num1*x,num2*x,num3*x);
		return; //my problem is here
	}
Gsterminator
Light Poster
31 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

If you plan on making your Vector3 class immutable, you have no option but to "return" Vector3 from your "multiply" method.

Without the restriction of immutable classes, it would be as simple as modifying the state variables of that given instance; you don't even need "v3".

public void multiple(int x) {
  this.num1 *= x;
  this.num2 *= x;
  this.num1 *= x;
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

wow! thanks soo much and it makes complete sense

Gsterminator
Light Poster
31 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: