简体   繁体   中英

Using an instance created in the main method of another Class

I am just starting with Java and ran into a problem:

Let's say I wanna have 3 classes, one we call "Cars" one is "Database" and one is a GUI to do stuff later on (Jframe-Form, Swing):

public class Cars {
    int nummer;
    String name;

    //lets just say i have 10 differnet types from 0  - 9 and every type has 
    // a different priceclass between 1 and 10 to make things simple

    public int[] types = new int[10];

    public Cars(int nr, String na) {
        this.nummer = nr;
        this.name = na;

I now want to create and save the cars in some kind of database, not sure how to do that most efficiently in Java so i just made a class like that:

public class Beispieldatenbank {

    public Cars[] c;

    public Beispieldatenbank (Cars[] c) {
        this.c = c;
    }

    public int getLengthc() {
        return(this.c.length);
    }

now i wanna create a database containing a few cars and assining types with their prices.

public static void main(String[] args) {
    Cars[] c1 = new Cars[100];
    Beispieldatenbank Beispieldatenbank1 = new Beispieldatenbank(c1);

    Cars Audi = new Cars(1, "Audi");
    Beispieldatenbank1.c[1] = Audi;
    Audi.type[0] = 1; //So the 0th type-Audi shall be in price class "1"
            Audi.types[1] = 3; //similarly...


    Cars BMW = new Cars(2, "BMW");
    Beispieldatenbank1.c[2] = BMW;
    BMW.type[0] = 5;

    etc...

I want the "Beispieldatenbank1" to be the one and only, publicly accesible instance of "Beispieldatenbank" and am now within my GUI:

           JButton btnEingabe = new JButton("Eingabe");
    btnEingabe.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String s = textField.getText();
            for(int i = 1, i < *Beispieldatenbank1.c.length()*, i++){
                ...              

I now wanna check if the input matches one of the cars so look whether "Audi" was typed for instance but:

i am in another (GUI) class, my Beispieldatenbank1 cannot be accessed!

Is this because "Beispieldatenbank1" only gets created later in the main method of "Beispieldatenbank" and is thereby not found/doesn't exist in the actionPeformed of my GUI? I thought since "Beispieldatenbank1" is public it'll be accessible from all other classes? How to get around this, implement this database easily? Whats the reason it is not there/accessible?

A public class means that you can refer it from any another class.
It doesn't mean that all instances of this public class can be accessed without variable referencing it. It would make no sense as we would not have any way to differentiate distinct instances of the class.

Your Gui class should store in a field the Beispieldatenbank instance created in the main() method

I want the "Beispieldatenbank1" to be the one and only, publicly accesible instance of "Beispieldatenbank"

You need a field if you want it to be accessible

private static Beispieldatenbank Beispieldatenbank1;

public static void main(String[] args) {
    Cars[] c1 = new Cars[100];
    Beispieldatenbank1 = new Beispieldatenbank(c1);

    // now you can access the variable class-wide

You need "the singleton pattern" to make only one instance of the class possible

i am in another (GUI) class, my Beispieldatenbank1 cannot be accessed!

Whenever you built the GUI class, you're required to pass the variable through the constructor or some setter method

Also, please do not capitalize variable names

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