简体   繁体   中英

Accessing objects made in main method from other classes

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();   

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();
    }
}

I am not exactly sure what you mean by "object" but I am just guessing that you mean variable.

In order for the variable declared in class to be accessible (in some way ie either directly or through some method) from outside it has to be a member variable not a local variable.

eg local (method-local) variable:

//local variables are the ones that are declared inside a method
//their life and visibility is limited only within the block in which they are define.
public static void main(String[] args) { // args is also a local variable
    String localVar = "Access modifiers are not allowed for local variables.";
    //the reason that the access modifiers are not allowed is because
    //the local variables are not members of the class        
}

It only makes sense to make the members of the class either private , protected , package-private, or public .

eg member variable:

class AClass {
    private String memberVar = "A member variable can have access modifier.";
    //the access modifier will determine the visibility of that variable.

    public String getMemberVar() {
        return this.memberVar;
    }
} 
  1. private: Visible only within the class. You can make it accessible by using some public method. Usually called getter methods.

  2. package-private: Visible only within the package. Again you can make it accessible outside the package by using some public method.

  3. protected: Visible only within the class and it's subclasses (does not matter even if the subclass is outside the package).

  4. public: Visible everywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM