non-static method problem: how to access non-static method from another .java file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xirowei
    New Member
    • Jun 2007
    • 17

    non-static method problem: how to access non-static method from another .java file?

    i now learning Object Oriented Programming but stuck. Need help. Below is the code.


    [CODE=java] PanelOption.jav a
    import java.awt.*;
    import java.awt.event. *;
    import javax.swing.*;
    import javax.swing.eve nt.*;

    public class PanelOption extends JPanel
    {
    //some code

    public PanelOption()
    {
    createOptionGUI ();
    }

    public void createOptionGUI ()
    {
    //some code
    }

    public class ButtonListener implements ActionListener
    {
    public void actionPerformed (ActionEvent e)
    {
    if (e.getSource()= =jbtnEnter)
    {
    PanelQuestion.Q uestion01(); // <= i wan to access to this void method but fail! How to solve?

    }
    }
    }
    }

    //=============== =============== =============== =============== ==========

    PanelQuestion.j ava
    import java.awt.*;
    import java.awt.event. *;
    import javax.swing.*;
    import javax.swing.eve nt.*;

    public class PanelQuestion extends JPanel
    {
    //some code

    public PanelQuestion()
    {
    createQuestionG UI();
    }

    public void createQuestionG UI()
    {
    //some code
    }

    public void Question01()
    {
    //some code (this is the void method that i want to invoke by PanelOption.jav a)
    }
    }[/CODE]
    Last edited by r035198x; Jun 14 '07, 03:14 PM. Reason: added code tags
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    Originally posted by xirowei
    i now learning Object Oriented Programming but stuck. Need help. Below is the code.


    PanelOption.jav a
    import java.awt.*;
    import java.awt.event. *;
    import javax.swing.*;
    import javax.swing.eve nt.*;

    public class PanelOption extends JPanel
    {
    //some code

    public PanelOption()
    {
    createOptionGUI ();
    }

    public void createOptionGUI ()
    {
    //some code
    }

    public class ButtonListener implements ActionListener
    {
    public void actionPerformed (ActionEvent e)
    {
    if (e.getSource()= =jbtnEnter)
    {
    PanelQuestion.Q uestion01(); // <= i wan to access to this void method but fail! How to solve?

    }
    }
    }
    }

    //=============== =============== =============== =============== ==========

    PanelQuestion.j ava
    import java.awt.*;
    import java.awt.event. *;
    import javax.swing.*;
    import javax.swing.eve nt.*;

    public class PanelQuestion extends JPanel
    {
    //some code

    public PanelQuestion()
    {
    createQuestionG UI();
    }

    public void createQuestionG UI()
    {
    //some code
    }

    public void Question01()
    {
    //some code (this is the void method that i want to invoke by PanelOption.jav a)
    }
    }
    Hi xirowei, welcome to TSDN,

    create an object of type PanelQuestion
    [CODE=java]
    PanelQuestion pq = new PanelQuestion() ;
    [/CODE]

    then call the the method
    [CODE=java]
    pq.Question01() ;
    [/CODE]

    all non-static methods must be perfixed with an object.

    good luck

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Consider the following scenario: my name is Jos, I'm an instantiation of the
      class SillyPerson. You can tell me things:

      [code=java]
      public void tell(String message) {
      ...
      }
      [/code]

      If you want to tell me something you have to do this:

      [code=java]
      SillyPerson jos= new SillyPerson();
      jos.tell("you are silly");
      [/code]

      and the message would be sent to me.

      You can't just do this:

      [code=java]
      SillyPerson.tel l("you are silly");
      [/code]

      because you are talking to an instance of the class SillyPerson (e.g. me) not
      to the class, the blueprint of all silly persons. In other words: you need at least
      an instantiation to talk to.

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        Consider the following scenario: my name is Jos, I'm an instantiation of the
        class SillyPerson. You can tell me things:

        [code=java]
        public void tell(String message) {
        ...
        }
        [/code]

        If you want to tell me something you have to do this:

        [code=java]
        SillyPerson jos= new SillyPerson();
        jos.tell("you are silly");
        [/code]

        and the message would be sent to me.

        You can't just do this:

        [code=java]
        SillyPerson.tel l("you are silly");
        [/code]

        because you are talking to an instance of the class SillyPerson (e.g. me) not
        to the class, the blueprint of all silly persons. In other words: you need at least
        an instantiation to talk to.

        kind regards,

        Jos
        Indeed welcome to TSDN. As you can see we have all sorts of people here. Now you see how your code looks under code tags? So next time you post code just wrap it inside code=java tags. the tags go inside square brackets []

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by r035198x
          As you can see we have all sorts of people here.
          No we don't:

          [code=java]
          Collection<Sill yPerson> TSDN= new ArrayList<Silly Person>();
          [/code]

          kind regards,

          Jos ;-)

          Comment

          • xirowei
            New Member
            • Jun 2007
            • 17

            #6
            epots9 thank you very much. You guide is really simple and easy to understand. Now it solve my problem of the non-static problem.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by xirowei
              epots9 thank you very much. You guide is really simple and easy to understand. Now it solve my problem of the non-static problem.
              Well the SillyPerson example really looked good too. It told quite a lot of true things which you would have done good to notice.

              Comment

              • epots9
                Recognized Expert Top Contributor
                • May 2007
                • 1352

                #8
                i won't disagree with u there r035198x, but still

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  :-)

                  kind regards,

                  Jos

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by JosAH
                    :-)

                    kind regards,

                    Jos
                    [CODE=java]TSDNAdmin nonSillyPerson = TSDN.getAdmin() ;
                    for(SillyPerson silly : TSDN) {
                    if(silly instanceOf Jos || silly instanceOf epots9) {
                    nonSillyPerson. ban(silly);
                    System.gc();
                    }
                    }[/CODE]

                    Comment

                    • epots9
                      Recognized Expert Top Contributor
                      • May 2007
                      • 1352

                      #11
                      Originally posted by r035198x
                      [CODE=java]TSDNAdmin nonSillyPerson = TSDN.getAdmin() ;
                      for(SillyPerson silly : TSDN) {
                      if(silly instanceOf Jos || silly instanceOf epots9) {
                      nonSillyPerson. ban(silly);
                      System.gc();
                      }
                      }[/CODE]
                      lol, that was good

                      lmao

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by r035198x
                        [CODE=java]TSDNAdmin nonSillyPerson = TSDN.getAdmin() ;
                        for(SillyPerson silly : TSDN) {
                        if(silly instanceOf Jos || silly instanceOf epots9) {
                        nonSillyPerson. ban(silly);
                        System.gc();
                        }
                        }[/CODE]
                        Ha! Code war!

                        [CODE=java]TSDNAdmin nonSillyPerson = TSDN.getAdmin() ;
                        for(SillyPerson silly : TSDN) {
                        if(silly instanceOf Jos || silly instanceOf epots9) {
                        nonSillyPerson. ban(silly= null);
                        System.gc();
                        }
                        }[/CODE]

                        kind regards,

                        Jos ;-)

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by JosAH
                          Ha! Code war!

                          [CODE=java]TSDNAdmin nonSillyPerson = TSDN.getAdmin() ;
                          for(SillyPerson silly : TSDN) {
                          if(silly instanceOf Jos || silly instanceOf epots9) {
                          nonSillyPerson. ban(silly= null);
                          System.gc();
                          }
                          }[/CODE]

                          kind regards,

                          Jos ;-)
                          [CODE=java]public boolean ban (SillyPerson silly) {
                          if(silly == null) {
                          for(SillyPerson silly : TSDN) {
                          if(silly instanceOf Jos || silly instanceOf epots9) {
                          silly.defenestr ate();
                          }
                          }
                          }
                          else {
                          silly.defenestr ate();
                          }
                          System.gc();
                          }
                          [/CODE]

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by r035198x
                            [CODE=java]public boolean ban (SillyPerson silly) {
                            if(silly == null) {
                            for(SillyPerson silly : TSDN) {
                            if(silly instanceOf Jos || silly instanceOf epots9) {
                            silly.defenestr ate();
                            }
                            }
                            }
                            else {
                            silly.defenestr ate();
                            }
                            System.gc();
                            }
                            [/CODE]
                            Ha! simple stuff:

                            [CODE=java]public boolean ban (SillyPerson silly) {
                            if(silly == null && silly != null) {
                            for(SillyPerson silly : TSDN) {
                            if(silly instanceOf Jos || silly instanceOf epots9) {
                            silly.defenestr ate();
                            }
                            }
                            }
                            else {
                            (silly= r035198x).defen estrate();
                            }
                            System.gc();
                            }
                            [/CODE]

                            kind regards,

                            Jos ;-)

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by JosAH
                              Ha! simple stuff:

                              [CODE=java]public boolean ban (SillyPerson silly) {
                              if(silly == null && silly != null) {
                              for(SillyPerson silly : TSDN) {
                              if(silly instanceOf Jos || silly instanceOf epots9) {
                              silly.defenestr ate();
                              }
                              }
                              }
                              else {
                              (silly= r035198x).defen estrate();
                              }
                              System.gc();
                              }
                              [/CODE]

                              kind regards,

                              Jos ;-)
                              Incompatible types: found TSDNAdminNonSil lyPerson required, SillyPerson (Jos)

                              Comment

                              Working...