Results 1 to 5 of 5
  1. #1
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default How to associate two classes?

    Hey,
    I have two classes.
    First and Second.

    In First class I want to use methods from Second class.
    So:
    Java Code:
    Second s = new Second();
    s.secondMethod();
    Second thing I want to do is use JTextArea from First class in Second class.
    So since it gives me error, I extended First class with Second:
    Java Code:
    public class Second extends First {
    It look like it should work, no errors etc. Also both things are working separately. But since I used both at once...
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    	at package.Second.<init>(Second.java:7)
    	at package.First.<init>(First.java:17)
    I can move what I need to First class, and it will work fine, but I want to make this in two classes.
    But I really don't understand extends, I just use them if there is need for them. So I don't know how to handle this problem.

    I also tried to extends Second just like First:
    Java Code:
    public class Second extends JPanel implements ActionListener {
    Instead of extending First, but it can not be done, since ActionListener is in First...


    Well. Also addActionListener can maybe solve my problem?
    I'm using:
    Java Code:
    xx.addActionListener(this);
    Maybe I can use something instead of this? But what? First? First.this? Idk...
    Last edited by Nozz; 04-24-2014 at 10:59 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default Re: How to associate two classes?

    Do each of the classes create instances of the other continuing it until... StackOverflowError
    Can First pass a reference: this to Second's constructor instead of having Second create an instance?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: How to associate two classes?

    I have no idea what are you talking about and I don't know what should I do with it...
    Can you explain it?

    Btw. Maybe there is better method to do what I want?

    I got some methods in my class. I want just to move them to another file. What can I do?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,394
    Rep Power
    25

    Default Re: How to associate two classes?

    To pass a reference of the First class to the Second class:

    In First:
    ... new Second(this); // pass an instance of the First class to the Second class

    In Second:
    public Second(First first) {
    this.first = first; // save reference to instance of the First class

    The use first to call First's methods.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Re: How to associate two classes?

    Had few problems, but somehow solved them.
    But still, I don't undestand 100% of that. So there is problem when I try to use more classes. I succeeded to use 3 classes associated, but need to practice to know exacly how to do this every time...
    But well, what can I except after 3-4 weeks? ;D

    Anyway. Thanks for help. Now I almost got it :)


    @edit
    Some problems occured:
    NullPointerException and ArrayIndexOutOfBoundsException...
    Last edited by Nozz; 04-25-2014 at 09:17 PM.

Similar Threads

  1. Replies: 0
    Last Post: 05-08-2012, 01:03 PM
  2. Replies: 0
    Last Post: 10-31-2011, 11:13 PM
  3. Replies: 2
    Last Post: 06-26-2011, 02:14 PM
  4. Sun Certified Java Associate
    By seedfox in forum Java Certification
    Replies: 4
    Last Post: 07-08-2009, 11:12 AM
  5. How do I associate a enum variable with an int?
    By frasifrasi in forum New To Java
    Replies: 2
    Last Post: 07-12-2008, 01:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •