Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:44:53+00:00 2026-05-27T18:44:53+00:00

Hey I’m trying to access an object I’ve made in the main method from

  • 0

Hey I’m trying to access an object I’ve made in the main method from another class but when I reference it in the other class it is not recognised. After some research I think this has something to do with access modifiers but I have tried making the object public only for a comment to appear saying “remove invalid modifier”. Any pointers?

Sorry about this being so basic but I’m only a beginner and I’m finding this stuff quite tough.

Sorry for not mentioning! I am writing in Java. This is what I have:

public static void main(String[] args) {

    Mainframe mainframe = new Mainframe();
    mainframe.initialiseMF();       
    LoginPanel lp = new LoginPanel();
    mainframe.add(lp);
}

public class Mainframe extends JFrame {

public Mainframe () {
    // Set size of mainframe
    setBounds(0, 0, 500, 500);

}

public void initialiseMF (){
    // Get the size of the screen
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the mainframe
    int w = getSize().width;
    int h = getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;

    // Move the mainframe
    setLocation(x, y);
    setVisible(true);
}

}

I am trying to do this statement in another class :

Container content = mainframe.getContentPane();   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:44:54+00:00Added an answer on May 27, 2026 at 6:44 pm

    Remember, the mainframe object is local to the main() method which is static. You cannot access it outside the class.

    Probably this would be a bit cleaner.

    public class Mainframe extends JFrame{
         public Mainframe(){
              initialiseMF ();
         }
    
         public void initialiseMF (){
              //do ur inits here
         }
    
    }
    

    Then do this,

    public class TheOtherClass{
    
        private Mainframe mainFrame;
    
        public TheOtherClass(){
            mainFrame = MainFrame.mainFrame; //although I would not suggest this, it will avoid the Main.mainFrame call
        }
    
        public void otherMethodFromOtherClass(JFrame mainFrame){
            Container content = mainFrame.getConentPane();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys I have a query that selects data and organizes but not in
Hey guys, am trying to write to do type casting in java, but i
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my
Hey having some trouble trying to maintain transparency on a png when i create
Hey, I need to do my drawing over a panel in C# but without
hey huys im counting my table with this code: $county = mysql_query(SELECT COUNT(id) from
Hey, I right now have a list of a struct that I made, I
Hey all I want to reseed my IDENTITY COLUMN values starting from 1 I
Hey all i have an XML file that i am trying to loop though.
Hey all i am probably overthinking this but how can i check the textbox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.