Results 1 to 13 of 13
- 11-06-2011, 12:35 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
Two classes: can someone tell me why this doesnt work?
Here's what I am trying to do: Have the user input a number, k, and then the program adds 10 to it. Simple enough. But Right now the program runs, it just always prints 10 no matter what number I put for k. So it basically ignores k. How can I fix this? Any help would be great, I'm still just learning about using different classes.
Java Code:import java.util.Scanner; public class TestingDriver{ public static void main(String args[]){ Scanner w=new Scanner(System.in);// user input of words per line System.out.println("enter number"); int k=w.nextInt(); Testing2 t=new Testing2(); t.test(); } }
Java Code:public class Testing2{ int k; public void test(){ System.out.println(k+10); } }
- 11-06-2011, 12:44 AM #2
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: Two classes: can someone tell me why this doesnt work?
Think about it, you want to increase 10 to the user input, for that you have to call to a method that increase a certain number in 10 and print it.
So this method must have one argument which is the user input.
Add to the test method to Test(int num) and print num+10.
Noted: you can have same variables names in diffrent classes and they will have diffrent values.
And if it all you want to do, why did you created a whole class for a single method?I would mix them into one class .. somthing like that:
Java Code:import java.util.Scanner; public class Exercices { public static void main(String[] args) { Scanner rd = new Scanner(System.in); int n = rd.nextInt(); print(n); } public static void print (int num) { System.out.println(num+10); } }Last edited by tnrh1; 11-06-2011 at 12:47 AM.
- 11-06-2011, 01:40 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
Re: Two classes: can someone tell me why this doesnt work?
Thank you! I got it...int k has to go into the parameters..
I know I don't need 2 classes, it was a simple example for something a little harder.Last edited by katiebear128; 11-06-2011 at 01:57 AM.
- 11-06-2011, 07:50 AM #4
Member
- Join Date
- Jul 2011
- Location
- New Delhi,India
- Posts
- 56
- Rep Power
- 0
Re: Two classes: can someone tell me why this doesnt work?
its the case of local variable.....the "k" you mentioned in different classes reference to different memory locations in the RAM....
take for example "k" in 1st class refer to memory location 1000 and in other class it refers to memory location 2000 in "YOUR CODE"....
so any change in value of "k" in the 2nd class wont be reflected in the value of "k" in 1st class...hence the unexpected output....
but the reply that you got from tnrh1 have k in both classes referencing the same memory location say 1000....this can be explained as:-
we allocate an int a memory location in 1st class say 1000,then we pass this memory location to the 2nd class.....the 2nd class catches this memory in its own method.....the method that catches this location will reference the same memory location by some other name say "num"....now 1000 is referenced by name "n" and "num"(you can say these are aliases)....now you can change content at 1000 by both the classes....
- 11-06-2011, 02:00 PM #5
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: Two classes: can someone tell me why this doesnt work?
Perfect explaination, 10/10.
- 11-06-2011, 04:02 PM #6
Member
- Join Date
- Jul 2011
- Location
- New Delhi,India
- Posts
- 56
- Rep Power
- 0
- 11-06-2011, 04:48 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Two classes: can someone tell me why this doesnt work?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-06-2011, 05:36 PM #8
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
Re: Two classes: can someone tell me why this doesnt work?
I'll simplfy it, he means that 2 variables in diffrent methods leades to the same memory.
- 11-06-2011, 05:46 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Two classes: can someone tell me why this doesnt work?
That is not correct; two different variables (whether in different classes or not) have different memory locations. Reference variablens ('pointers') can refer to the same object and hence to identical memory locations. Cosmos's explanation was simply not correct. All variables (whether reference variables or primitive variables) can have identical values of course.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-06-2011, 06:13 PM #10
Member
- Join Date
- Jul 2011
- Location
- New Delhi,India
- Posts
- 56
- Rep Power
- 0
Re: Two classes: can someone tell me why this doesnt work?
i meant to say that i passed the memory reference for 1000 memory location to the method residing in some other class....now the other class will reference the same memory location with some other name like "num"....now "num" and "k" will refer to same memory location...i think i am right now.....
kind regards,
Cosmos
- 11-06-2011, 06:36 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Two classes: can someone tell me why this doesnt work?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-06-2011, 11:17 PM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Two classes: can someone tell me why this doesnt work?
Have a read of the JavaRanch discussion of parameter passing. (At that link and the one it continues with).
When you say foo(bar) then foo() ends up with a copy of the variable bar's value which it can assign to any variable it likes. Variables and their values (together with the ability of one variable to end up with an independent copy of another's value) is enough: "memory locations" are not expressible in the Java language.
- 11-08-2011, 03:08 AM #13
Member
- Join Date
- Sep 2011
- Posts
- 54
- Rep Power
- 0
Similar Threads
-
relation doesnt work
By Nigel in forum New To JavaReplies: 3Last Post: 03-12-2011, 07:02 PM -
KeyListener doesnt work some times.
By Addez in forum New To JavaReplies: 15Last Post: 09-21-2010, 04:25 PM -
PrintWriter doesnt work :(
By Addez in forum New To JavaReplies: 11Last Post: 01-17-2010, 05:59 PM -
Dll Call doesnt work
By INFACT in forum New To JavaReplies: 1Last Post: 10-04-2009, 09:31 PM -
java doesnt allow vista to work
By 10rosas in forum New To JavaReplies: 5Last Post: 12-22-2008, 04:23 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks