Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to use the same instance of an object in two methods

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default How to use the same instance of an object in two methods

    My issue is that in my code, I create a new JFrame object called frame. In another method I want to close that frame. Any suggestions?

    // Non-relevant code
    private static class oddbutton implements ActionListener{
        public void actionPerformed(ActionEvent e) {
     
     
          }
    }
    //More Non-relevant code
     public void run(){
            NewGame.setoddnumber();
            NewGame.setevennumber();
            NewGame.separate();
            JFrame frame = new JFrame("Button Game!");
                  addComponentsToPane(frame.getContentPane());
                     Insets insets = frame.getInsets();
            frame.setSize(200, 300);
            frame.setVisible(true);
        }

    As of now, I have nothing in my oddbutton class. I don't know how to close the frame.


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    20
    Thanks
    4
    Thanked 3 Times in 3 Posts

    Default Re: How to use the same instance of an object in two methods

    Never mind. I solved my issue. All I had to do was make frame a static public object.

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to use the same instance of an object in two methods

    Making something static to get around accessibility issues is a bad solution.
    Improving the world one idiot at a time!

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: How to use the same instance of an object in two methods

    Quote Originally Posted by Junky View Post
    Making something static to get around accessibility issues is a bad solution.
    So, what else can be the solution to OP's specified problem?

Similar Threads

  1. Overriding Object methods
    By tarkal in forum Java Theory & Questions
    Replies: 2
    Last Post: November 10th, 2011, 07:46 AM
  2. [SOLVED] Instance data member vs Local variables (primitive/object reference)
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 12:42 AM
  3. Newb question: Using the same object over two methods?
    By CitizenSmif in forum Object Oriented Programming
    Replies: 5
    Last Post: July 3rd, 2011, 08:17 PM
  4. [SOLVED] Help with Classes/ Instance Methods
    By Stockholm Syndrome in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 9th, 2011, 06:33 PM
  5. [SOLVED] Help with classes & instance methods
    By ShakeyJakey in forum Object Oriented Programming
    Replies: 16
    Last Post: July 30th, 2010, 01:20 PM