Results 1 to 9 of 9
  1. #1
    67726e is offline Member
    Join Date
    Mar 2010
    Location
    Hilton Head, SC
    Posts
    34
    Rep Power
    0

    Default Calling an Object

    Ok, if I have an object A, how would I go about calling it from object B?

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Be more specific.

    Does A have methods? Do you want to call a constructor?

    Java Code:
    public class B {
        public B() {
            A a = new A(); // calls constructor
            a.someMethod(); // calls method "someMethod"
        }
    }
    Something like that?

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Do you mean "class" A and B, not object? If so, then

    Java Code:
    class A {
       public void someMethodOfA() {
          System.out.println("in someMethodOfA");
       }
    }
    
    class B {
       private A aObject = new A();
    
       public void someMethod() {
          aObject.someMethodOfA();
       }
    }

    If this is not what you meant, then you will need to clarify your question a lot.

  4. #4
    67726e is offline Member
    Join Date
    Mar 2010
    Location
    Hilton Head, SC
    Posts
    34
    Rep Power
    0

    Default

    Let me give a more detailed exmaple:

    Java Code:
    public class RSS {
         public static void main(String[] args) {
              Frame frame = new Frame();
              frame.setDeafultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setVisible(true);
              frame.setResizable(false);
         }
    }
    
    class Frame extends JFrame {
         public Frame() {
              setBounds(/* location, size */);
              setTitle("Some Text");
    
              Container contentPane = getContentPane();
              JPanel panel = new Panel();
              contentPane.add(panel);
              panel.setLayout(null);
         }
    }
    
    class Panel extends JPanel {
         public Panel() {
              Menu menu = new Menu();
              add(menu);
    
              text = new JTextField();
              text.setBounds(/* Size it, position it */);
              add(text);
         }
    
         public String getText() {
              return text.getText();
         }
    
         private JTextField text;
    }
    
    class Menu extends JMenuBar {
         public Menu() {
              setBounds(/* locate, size it */);
              
              JMenu file = new JMenu("File");
              JMenuItem item = new JMenuItem("Do Stuff");
              file.add(item);
    
              item.addActionListener(new ActionListener() {
                   public void actionPerformed(ActionEvent event) {
                        String str = panel.getText();
                   }
              });
         }
    }
    The code gives me the error:

    RSS.java:53: cannot find symbol
    symbol: variable panel
    String str = panel.getText();
    ^
    1 error

    I know I can do it statically by calling Panel.getText(); and making text a static JTextField, but is there a way to do it they way I have above?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Pass a reference of the Panel object into Menu via a constructor parameter, and call the method off of that.

    e.g.,

    Java Code:
    class Panel extends JPanel {
       public Panel() {
          Menu menu = new Menu(this); //!! *** note change ****
          add(menu);
    
          // .... code deleted
       }
    
       //...   
    }
    
    class Menu extends JMenuBar {
       Panel myPanel;
    
       public Menu(Panel p) {
          myPanel = p; // get your reference and assign it.
          // ....
    
          item.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent event) {
                // !! String str = panel.getText();
                String str = myPanel.getText();
             }
          });
       }
    }
    Last edited by Fubarable; 08-10-2010 at 12:05 AM.

  6. #6
    crikey is offline Senior Member
    Join Date
    Jul 2010
    Posts
    125
    Rep Power
    0

    Default

    make myPanel static. Then it should work

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by crikey View Post
    make myPanel static. Then it should work
    To the original poster, please do not follow this route as it will destroy any object-oriented abilities of your program.

    To crikey: Please stop giving out bad advice (as here secondary-jframe). This is worse than giving no advice at all. And don't get me wrong, I'm not saying I'm perfect, far from it, but at least I state when I don't know what the heck I'm talking about or when I'm guessing.
    Last edited by Fubarable; 08-10-2010 at 01:16 AM.

  8. #8
    67726e is offline Member
    Join Date
    Mar 2010
    Location
    Hilton Head, SC
    Posts
    34
    Rep Power
    0

    Default

    Passing references! I do that all the time with event delegation in jQuery yet I never realize it is the same concept. Thank you so much!

  9. #9
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    To the original poster, please do not follow this route as it will destroy any object-oriented abilities of your program.

    To crikey: Please stop giving out bad advice (as here secondary-jframe). This is worse than giving no advice at all. And don't get me wrong, I'm not saying I'm perfect, far from it, but at least I state when I don't know what the heck I'm talking about or when I'm guessing.
    Fubarable, could you elaborate some more on your comments about declaring things static? i'm very interested in hearing your opinions on this.
    Teaching myself java so that i can eventually join the industry! Started in June 2010

Similar Threads

  1. same object variable t and same methods is calling
    By javastuden in forum New To Java
    Replies: 1
    Last Post: 11-24-2009, 05:10 AM
  2. Calling Object.method
    By Fedor in forum New To Java
    Replies: 1
    Last Post: 04-11-2009, 02:44 PM
  3. 'Class' Object and calling Static Methods?
    By mikeiz404 in forum Advanced Java
    Replies: 3
    Last Post: 01-24-2009, 01:58 PM
  4. Calling a method for all instances of an object
    By rattle in forum New To Java
    Replies: 4
    Last Post: 04-30-2008, 03:10 PM
  5. Object from String (calling method dynamically)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 10:22 PM

Posting Permissions

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