2

myClass1:

public class myClass1
{
    public myClass2 myclass2;

    public void createsecondclass (String[] args)
    {
        myclass2 = new myClass2(this);
        myclass2.dosomething();
    }

myClass2:

public class myClass2
{
    public myclass1;

    public myClass2(myClass1 myclass1)
    {
        this.myclass1 = myclass1;
    }

    public void dosomething()
    {
        myclass1.another_object_that_could_be_placed_here.dosomething();
    }
}

would this not make the code cleaner when trying to access a large amount of objects which all in one way or another are all instantiated under a single class? i ask because i am trying to learn libGDX and in the large assortment of class files that are made to handle each element of the game it just seems easier to pass my application listener down the line since the application listener contains the screen which contains the gameworld which contains the player and so on...

But the problem that i worry about is that by setting it to a variable in the object i am creating a myclass1 that contains a myclass2 that contains a myclass1 and so on. i worry that this might cause memory leaks and since my target is android, memory is a big concern.

if anyone has any thoughts on the subject, directly relevent or not i would appreciate the input. i am after all still learning.

thanks =)

  • i would also like to point out that the class is being invoked from a pre-fabricated libGDX class that extends "Game" however all of that isnt needed to get the idea across and thus was excluded.... – Konner Rasmussen Jul 05 '13 at 18:00
  • 2
    I feel the pseudo code doesn't really explain what you are trying to do. What is the problem you want to solve? – Joni Jul 05 '13 at 18:02
  • "would this not make the code cleaner" - what is "this" referring to here? – selig Jul 05 '13 at 18:03
  • @Joni i am wondering if passing the first class into the second will cause there to be multiple instances of the object in ram... and if something similar to this would be a memory hog – Konner Rasmussen Jul 05 '13 at 18:04
  • @selig "this" will always refer to the class that "this" is being used in... for example if you have a class variable called "myvariable" AND an argument called "myvariable"... "this.myvariable" will point to the class variable and "myvariable" will refer to the local variable – Konner Rasmussen Jul 05 '13 at 18:06
  • No, passing an object reference to a method or constructor does not copy the object. – Joni Jul 06 '13 at 08:43

0 Answers0