6 Replies - 103 Views - Last Post: 08 February 2012 - 11:23 AM Rate Topic: -----

Topic Sponsor:

#1 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 27-January 10

Changing a Variable in Java

Posted 08 February 2012 - 09:02 AM

Hi, I was wondering how a variable can be changed in one method and then used in another. Here is a simple example:

public class Example {
    public int num, num2;

public Example () {
    num = 10;
}

public void method1 () {
    num2 = num + 1;  
    System.out.println(num2);
}

public void method2 () {
    num = 20;
}
}



When I call the first method, I get 11 (as I should). However, after calling the second method and then the first again, I expect to get 21. This does not happen. How do I edit this code so that variables can be changed in one method and used in another? Thanks in advance.

This post has been edited by ninjawesome222: 08 February 2012 - 09:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Changing a Variable in Java

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Changing a Variable in Java

Posted 08 February 2012 - 09:14 AM

Since your variable is a class variable, changing it in one method will affect the variable no matter what. May be we can see your main() and see how you were trying to test your code. But if I have:
Example e = new Example();
e.method1 (); //print 11
e.method2 (); //set num = 20
e.method1 (); //print 21

What have you tried to do, is like this?
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1788
  • View blog
  • Posts: 3,361
  • Joined: 19-March 11

Re: Changing a Variable in Java

Posted 08 February 2012 - 09:18 AM

Quote

However, after calling the second method and then the first again, I expect to get 21. This does not happen


I find that hard to believe.

Given this main method:

public static void main(String[] args)
    {
    	Test test = new Test();
    	test.method1();
    	test.method2();
    	test.method1();
    }


I get this output:

11
21
Was This Post Helpful? 0
  • +
  • -

#4 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 27-January 10

Re: Changing a Variable in Java

Posted 08 February 2012 - 09:29 AM

Here is an excerpt of my actual code that shows only relevant methods/variables. Upon running (I am using Eclipse), the output is 9 and then 9 again when it should be 9 and then 30. This program is to simulate a public city bus by the way.

public class Bus {
	// Local Variables
	public int standCap, trueStandCap;

public Bus () {
	
        trueStandCap     = 30;                                      // max number of standing people that can fit on bus
	standCap         = (int)(0.33 * trueStandCap);              // first driver's perceived cap
}

public int getPercievedCapacity () {							// designed to return the driver's perceived standing capacity
	System.out.print("Perceived Capacity: ");
	return standCap;

public void changeDriver (float percent) {						// designed to change the perceived standing capacity
	if (percent >= 0.33 && percent <= 0.1) {
	 int temp = Math.round(percent * 100);
	 standCap = (int)(seatCap * temp / 100);
	}
}

public static void main (String[] args) {						
	Bus b1 = new Bus();
	float percentInDecimal = 0.1f;
	
	System.out.println(b1.getPercievedCapacity());
	b1.changeDriver(percentInDecimal);
	System.out.println(b1.getPercievedCapacity());
}
}


This post has been edited by ninjawesome222: 08 February 2012 - 09:35 AM

Was This Post Helpful? 0
  • +
  • -

#5 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 27-January 10

Re: Changing a Variable in Java

Posted 08 February 2012 - 10:52 AM

I found my error! It was in this line:
if (percent >= 0.33 && percent <= 0.1)


obviously, a number cannot be greater than 0.33 AND less than 0.1 as I have written. Because of this, the if statement is never executed. The 0.1 should be a 1.0 instead. After changing the line this
if (percent >= 0.33 && percent <= 1.0)


the code worked perfectly. Thank you all for your help and I am very surprised that I got worked up over such a simple error. =D
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1788
  • View blog
  • Posts: 3,361
  • Joined: 19-March 11

Re: Changing a Variable in Java

Posted 08 February 2012 - 10:57 AM

Yeah, that's why it's nice if you give us the actual code you're having trouble with.
Was This Post Helpful? 0
  • +
  • -

#7 ninjawesome222  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 27-January 10

Re: Changing a Variable in Java

Posted 08 February 2012 - 11:23 AM

I know, but my code is quite a mouthful (at least it is in its entirety) so I just wanted to give a quick example. I guess, what I was wanting to know was not relevant to my actual problem. Thanks for the assistance though. I really appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1