-1

Ok, For Instance I have a main class which extends JFrame like the below one; and also this class create an object of another class called Adjust.

public class Gui extends JFrame{
    public Gui(){
         ... // There are some codes
         Adjust adjust = new Adjust();
         int length = adjust.getLength();
    }
    public static void main(String[] args){
    Gui input = new Gui();
    input.setVisible(true);
    input.pack();

    }
}

In Adjust Class, I need to have access to Gui class to get some information in order to produce the getLength method. In order to do that, I created an object of Gui class in Adjust class like the below one:

public class Adjust{
    private int info;
    public Adjust(){
        Gui gui = new Gui();
        this.info = gui.getInformation();
        ... // There are some additional codes
    }
    public int getLength(){
        // in this method, I do some processes to provide the length based on information in gui
        return ...
   }
 }

So, In adjust class, when I want to access Gui class, these classes repeatedly switch between them selves and are not moved to the next lines of codes. I also extends the Adjust class to Gui class, but it again happened and classes infinitely switches between each other. I do not want to create static instances and variables in Gui class in order to have direct access in Adjust class. Please help me where the problem is? If you feel there are some confusions in my explanation, please do let me know if I can make it better.

Here is more information. ==>I don't want to make the third class to combine the other two classes. The reason I want to do this, because I will use the Adjust object in other classes to access the getLength() method in Adjust class created based on the information in Gui class.

Mahdi
  • 55
  • 2
  • 10
  • @JarrodRoberson I don't think this is a dup, as the OP states what he/she is _not_ trying to do (use `static` references), and is showing us what their attempt is. Though it's obvious _what_ the problem is, the dup, you've provided doesn't actually solve what the OP is trying to do, i.e. "If I can't do this, what can I do, without using static"? Though the OP didn't actually state this request, IMO it is implied :-) – Paul Samsotha Nov 03 '14 at 03:23
  • @peeskillet, Thx for making my question more clear. As you said, this is not a dup. As I said I do not want to use static structure. – Mahdi Nov 03 '14 at 04:36
  • @JarrodRoberson, Buddy, This is not a dup, the reference you attached has used static version. I do not want to make my instances and variables static. I want to use the same name variables in different classes and want to have a clean code by making objects. – Mahdi Nov 03 '14 at 04:39
  • You should edit your post to make your intentions more clear. – Paul Samsotha Nov 03 '14 at 04:53
  • Also I really think you need to provide more context as to what is it you _really_ want to accomplish. In essence, from your code snippet, it just looks like you want to set the value in `Adjust`, and immediately returning it back to the `Gui`. WHY? What is the purpose. Why not just use the value from the `Gui`? It makes no sense. – Paul Samsotha Nov 03 '14 at 05:13

2 Answers2

4

You have called Adjust() function in the GUI

public Gui(){
     ... // There are some codes
     Adjust adjust = new Adjust();

and then again called the Gui in Adjast

 public Adjust(){
    Gui gui = new Gui();
    this.info = gui.getInformation();

So, it becomes a infinite call.

To solve, you should stop this kind of nested call.

Abrar Jahin
  • 13,970
  • 24
  • 112
  • 161
0

It seems like you could pass info to your Adjust Constructor. That should break the cycle. Basically, something like

public class Gui extends JFrame{
    public Gui(){
         Adjust adjust = new Adjust(this.getInformation());
         ... // There are some codes
         int length = adjust.getLength();
    }
    public static void main(String[] args){
        Gui input = new Gui();
        input.setVisible(true);
        input.pack();
    }
}

Then

public Adjust(int info) {
    // Gui gui = new Gui();
    // this.info = gui.getInformation();
    this.info = info;
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I can pass it to constructor, but what if I have like 10 variables. The constructors will look like very vague and lengthy. you may answer me that I can set up 10 different constructors in adjust class, but this again makes my code un-readable and also, I really want to work with objects. – Mahdi Nov 03 '14 at 13:26
  • You could pass a [POJO](http://en.wikipedia.org/wiki/Plain_Old_Java_Object) to the `Adjust` constructor. Normally, you might pass `Gui` but you can't from the `Gui` constructor. – Elliott Frisch Nov 03 '14 at 13:28
  • Make your *hypothetical* "10 variables" fields of a custom class. Pass that class. If you can use `Adjust` *after* you've finished constructing `Gui` you could pass your `Gui` to the `Adjust` constructor, and have it call methods in `Gui` (but the order matters, you have to finish constructing the `Gui` instance or you'll have another cycle). – Elliott Frisch Nov 03 '14 at 14:28