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?