Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By Cosmos

Thread: Two classes: can someone tell me why this doesnt work?

  1. #1
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default 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);
      }
    }

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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.

  3. #3
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default 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.

  4. #4
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default 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....
    katiebear128 likes this.

  5. #5
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: Two classes: can someone tell me why this doesnt work?

    Perfect explaination, 10/10.

  6. #6
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: Two classes: can someone tell me why this doesnt work?

    Quote Originally Posted by tnrh1 View Post
    Perfect explaination, 10/10.
    thanks...:)

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Two classes: can someone tell me why this doesnt work?

    Quote Originally Posted by Cosmos View Post
    we allocate an int a memory location in 1st class say 1000,then we pass this memory location to the 2nd class
    If memory location 1000 is taken by an int variable you can't pass the memory location to something else; that's what pass by value is all about; bad example.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default 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.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Two classes: can someone tell me why this doesnt work?

    Quote Originally Posted by tnrh1 View Post
    I'll simplfy it, he means that 2 variables in diffrent methods leades to the same memory.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: Two classes: can someone tell me why this doesnt work?

    Quote Originally Posted by JosAH View Post
    If memory location 1000 is taken by an int variable you can't pass the memory location to something else; that's what pass by value is all about; bad example.

    kind regards,

    Jos
    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. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Two classes: can someone tell me why this doesnt work?

    Quote Originally Posted by Cosmos View Post
    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.....
    No you're not; if memory location 1000 is taken by a primitive variable there is no way in Java that you can pass 1000 to another method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  13. #13
    katiebear128 is offline Member
    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    0

    Default Re: Two classes: can someone tell me why this doesnt work?

    I read the JavaRanch, it was helpful. :)
    Thanks everyone for the help!

Similar Threads

  1. relation doesnt work
    By Nigel in forum New To Java
    Replies: 3
    Last Post: 03-12-2011, 07:02 PM
  2. KeyListener doesnt work some times.
    By Addez in forum New To Java
    Replies: 15
    Last Post: 09-21-2010, 04:25 PM
  3. PrintWriter doesnt work :(
    By Addez in forum New To Java
    Replies: 11
    Last Post: 01-17-2010, 05:59 PM
  4. Dll Call doesnt work
    By INFACT in forum New To Java
    Replies: 1
    Last Post: 10-04-2009, 09:31 PM
  5. java doesnt allow vista to work
    By 10rosas in forum New To Java
    Replies: 5
    Last Post: 12-22-2008, 04:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •