Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Transfering Variables across classes.

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Transfering Variables across classes.

    Class 1:

    view sourceprint?
    1 public class one {

    2 public static void main(String[] args){

    3 int num = 0;

    4 System.out.println(num);

    5 two.change(num);

    6 System.out.println(num);

    7 }

    8 }


    Class 2:
    view sourceprint?
    1 public class two {

    2 static void change(int num){

    3 num = num + 1;

    4 }

    }

    This should print 0, then 1, but it prints 0, then 0. How do I make it print 0 then 1 with the same format?


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Transfering Variables across classes.

    That code is very hard to read, but from what I can gather, there is no
    instance of class two being created, so your call to:

    two.change(num);
    Should of caused an error. Both classes I am assuming are also in separate source files,
    so make sure they each have the same package name so the compiler knows where to find
    the methods of each class when they are invoked.

    Thirdly, Java does not pass values by reference, only by value. Hence if you passed 'num'
    to class two;s method which attempts to increment it by one, the change would happen in
    that method, but when control is returned to first method to print out the result, the original
    value of 'num' is the same - hence why it is printing zero.

    One way around this is to create an array holding the value, and pass that to the method then modify
    the index. Array's are passed by reference, class objects by pointer reference.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Transfering Variables across classes.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

    How did you come by this code? Did you just think of it? It's an interesting study that you should play around with to gain a better understanding of how classes and methods work together - or don't. Play with nested classes, static nested classes and inner classes, static/non-static methods, etc. You can find out more about these topics in the Oracle Tutorials.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Transfering Variables across classes.

    Quote Originally Posted by Ada Lovelace View Post
    so your call to:



    Should of caused an error.
    The change method is static so the code compiles fine. Of course had the OP adhered to naming conventions and called the class Two it would have been easier to read.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 2
    Last Post: December 14th, 2012, 05:31 AM
  2. Accessing Variables Between Classes
    By thudgun in forum Object Oriented Programming
    Replies: 9
    Last Post: January 2nd, 2012, 01:08 AM
  3. Changing Array variables from different classes
    By smellyhole85 in forum Collections and Generics
    Replies: 6
    Last Post: December 9th, 2010, 03:18 PM
  4. using variables from other classes in arrays :/
    By mozyman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2010, 11:26 PM
  5. State Variables interaction with outside classes?
    By Ace Coder in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2010, 03:52 PM