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 3 of 3

Thread: Passing reference to multiple classes in java

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Passing reference to multiple classes in java

    Hello,

    I am a beginner in java programming basically, I am trying to make my code object-oriented. I have a set of 3 games in which all of them have players, frame, board, buttons etc. what I want is instead of declaring in every game the buttons and the board or the player, etc, again and again, I want to reference the class with the players or the board, etc. how am i supposed to do that?

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Passing reference to multiple classes in java

    I'm not certain I fully understand the overall design. However, classes may be shared between projects and you may also pass instances of a class to other classes via methods. One example might be a checkers and a chess game. There is no need to have two board classes since they are both the same.
    In both chess and checkers you could say

    Board board = new Board();

    and use that board instance for the game.

    Regards,
    Jim

  3. #3
    Member
    Join Date
    Sep 2018
    Posts
    32
    Thanks
    0
    Thanked 8 Times in 6 Posts

    Default Re: Passing reference to multiple classes in java

    Java is strictly pass-by-value, it's just that the values are often references.) When you change the value within that object using theBar.foo.a , then looking at the value of a again using theFoo.a will see the updated value. Basically, Java doesn't copy objects unless you really ask it to.
    public class FooClass {
    BarClass bar = null;
    int a = 0;
    int b = 1;
    int c = 2;

    public FooClass(BarClass bar) {
    this.bar = bar;
    bar.setFoo(this);
    }
    }

    public class BarClass {
    FooClass foo = null;

    public BarClass(){}

    public void setFoo(FooClass foo) {
    this.foo = foo;
    }
    }
    elsewhere...

    BarClass theBar = new BarClass();
    FooClass theFoo = new FooClass(theBar);
    theFoo.a //should be 0
    theBar.foo.a = 234; //I change the variable through theBar. Imagine all the variables are private and there are getters/setters.

    theFoo.a //should be 234 <-----
    How can I pass an object to another class, make a change, and have that change appear in the original instance of the first object?

Similar Threads

  1. Passing by reference in Java?
    By ineedahero in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 14th, 2013, 09:50 AM
  2. [SOLVED] Pass-by-Value scheme, puzzled with passing/assigning a reference to another object
    By chronoz13 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 2nd, 2012, 10:43 AM
  3. PrintWriter: passing File reference vs String
    By kahwa in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 12th, 2012, 12:56 AM
  4. Passing reference via object
    By Stefan_Lam in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2011, 10:57 AM
  5. Passing Information between classes
    By SKhoujinian91 in forum Object Oriented Programming
    Replies: 4
    Last Post: December 8th, 2009, 02:40 PM