Hi,
I'm sorry for making the question unclear, but my English is not good.
I will try to explain it:

class test0{
	test1 t1 = new test1();
	public static void main(String args[]){
		new test0();
	}
	public test0(){		
		t1.displaymsg1();
		t1.callDisplaymsg0();
	}
	public void displaymsg0(){
		System.out.println("Successfully linked to test0 class");
	}
}

class test1{
	test0 t0 = new test0();
	public void callDisplaymsg0(){
		t0.displaymsg0();
	}
	public void displaymsg1(){
		System.out.println("Successfully linked to test1 class");
	}
}

Class test0 can "new test1()", and access its members. But when I try to "new test0()" in class test1, to access class test0's members, it floods the screen with many lines of errors like:
at test1.<init>(test0.java:16)
at test0.<init>(test0.java:2)
at test1.<init>(test0.java:16)
at test0.<init>(test0.java:2)

and I could not read the first lines of the error, so I don't know what the error is..

So my question is:
How to access class test0 members from class test1?

Thanks a lot in advance!

I got it. Here is the thing. The error you get is this:

Exception in thread "main" java.lang.StackOverflowError

If I remember correctly it usually happens when you run out of memory due some "endless loop" thing. Please someone correct me if I am wrong.

And here is the practical reason why you get an error:

In main you do this: new test0(); You create a new instance of test0. What happens when you do that? : You create a new instance of test1:

test1 t1 = new test1();
public static void main(String args[] ){
new test0()
}

What happens when you create a test1 object? :
You create a test0 object
class test1{
test0 t0 = new test0()
....
}

Then the above code calls the constructor of test0 (a NEW instance of test0) therefor a new test1 is created :
class test0{
test1 t1 = new test1();
....
}

And when you created this test1 another test0 is created, then another test1 is created then another test0 is created, .... then you get that overflow thing.

You created an 'endless loop'.
It was like doing this:

public void method1() {
    System.out.println("method1");
    method2();
}

public void method2() {
    System.out.println("method2");
    method1();
}

Execute the above code and see what happens

Thank you for responding!

I guess that was because of the endless loop too, but still can't figure it out how to fix that..

What exactly are you trying to accomplish?

I have two frames in two classes, I need to make the link button, that when I click the button on first frame, it will close the first frame and open the second frame, and when I click the button on the second frame, it closes the second frame and open back the first frame.

Version1:

class Frame1 extends JFrame {
  
  public Frame1() {
     
  }   

  private void methodClose1_Open2() {
      this.setVisible(false);
      Frame2 frame2 = new Frame2(this);
      frame2.setVisible(true);
  }
}
class Frame2 extends JFrame {
  Frame1 frame1 = null;

  public Frame2(Frame1 frame1) {
      this.frame1 = frame1;
  }   

   private void methodClose2_Open1() {
      this.setVisible(false);
      frame1.setVisisble(true);
  } 
}

Version 1:
As you can see Frame2 does NOT call a constructor. It uses the 'previous' frame that opened it (frame1).
Frame2 takes as argument a Frame1 object so you need to put the existing frame, not create a new:

Frame2 frame2 = new Frame2(this)
frame2.setVisible(true)

OR Version 2 of Frame1:

class Frame1 extends JFrame {
  Frame2 frame2 = null;

  public Frame1() {
     this.frame2 = new Frame2(this);
  }   

  private void methodClose1_Open2() {
      this.setVisible(false);
      frame2.setVisible(true);
  }
}

Again, the Frame1 constructor will be called ONCE therefor the Frame2 will be called ONCE.
Then you just use the instances to change the visibility of the frames

Also a second example:

class FrameMaster extends JFrame {
  Frame1 frame1;
  Frame2 frame2;

// create somehow the 2 frames
public FrameMaster() {
  frame1 = new Frame1(this);
  frame2 = new Frame2(this);
}


 void openFrame1() {
   this.setVisible(false);
   frame1.setVisible(true);
 }

 void openFrame2() {
   this.setVisible(false);
   frame2.setVisible(true);
 }
}
class Frame1 extends JFrame {
  FrameMaster master;

 public Frame1(FrameMaster master) {
  this.master = master;
  } 

 void openMasterFrame() {
   this.setVisible(false);
   master.setVisible(true);
 }
}

class Frame2 extends JFrame {
  FrameMaster master;

 public Frame2(FrameMaster master) {
  this.master = master;
  } 

 void openMasterFrame() {
   this.setVisible(false);
   master.setVisible(true);
 }
}

Wow thanks! Thanks a lot for your help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.