Reputation: 25
i defined a class TestoMessaggi and a subclass called Messaggio
//esercizio 3.3 del libro
import javax.swing.JOptionPane;
public class TestoMessaggio {
private String code;
private String testo;
public TestoMessaggio(String c, String t) {
code = c;
testo = t;
}
public static TestoMessaggio creaTestoMessaggio() {
String co = JOptionPane.showInputDialog(null,"inserisci codice");
String te = JOptionPane.showInputDialog(null,"inserisci testo");
TestoMessaggio t1 = new TestoMessaggio(co,te);
return t1;
}
public String getCode() {
return code;
}
public String getTesto() {
return testo;
}
}
Here is Messaggio.class
public class Messaggio extends TestoMessaggio {
private String mittente;
private String destinatario;
public Messaggio(String c, String t,String m, String d) {
super(c,t);
mittente = m;
destinatario = d;
}
public String getDestinatario() {
return destinatario;
}
public String getMittente() {
return mittente;
}
public void setDestinatario(String d) {
destinatario = d;
}
public static void stampaMessaggio(Messaggio m) {
System.out.println("code : "+m.getCode());
System.out.println("testo : "+m.getTesto());
System.out.println("destinatario : " +m.getDestinatario());
System.out.println("mittente : " +m.getMittente());
}
}
i created a program to test the two classes: here's the code
//esercizio 3.5 del libro
import javax.swing.JOptionPane;
public class Esempio3_5 {
public static String leggiNumero() {
String num = JOptionPane.showInputDialog(null,"inserisci numero");
return num;
}
public static void main(String[] args) {
String m = leggiNumero();
TestoMessaggio t1 = creaTestoMessaggio(); // non trova il metodo
String d = leggiNumero();
Messaggio mex = new Messaggio(null,null, m,d);
stampaMessaggio(mex); // nn trova il metodo
}
}
when i try to compile i get this error
Esempio3_5.java:16: error: cannot find symbol
TestoMessaggio t1 = creaTestoMessaggio(); // non trova il metodo
^
symbol: method creaTestoMessaggio()
location: class Esempio3_5
Esempio3_5.java:19: error: cannot find symbol
stampaMessaggio(mex); // nn trova il metodo
^
symbol: method stampaMessaggio(Messaggio) location: class Esempio3_5
all 3 files are in the same directory. Any suggestions? Thanks in advance
Upvotes: 0
Views: 4473
Reputation: 9
it is not getting the functions from the other classes. try to associate them using dot(.)opertaor like TestoMessaggio t1 = TestoMessaggio.creaTestoMessaggio();
Upvotes: 0
Reputation: 40315
Your error says:
Esempio3_5.java:19: error: cannot find symbol
stampaMessaggio(mex); // nn trova il metodo
I don't see a stampaMessagio()
method in Esempio3_5
, and neither does the compiler. When you call a method without a class name or instance before it, it calls the method in the current class/object.
I'm presuming you want to call the static stampaMessagio()
that's defined in Messagio
, so you'd need to actually do that:
Messaggio.stampaMessaggio(mex); // nn trova il metodo
Upvotes: 0
Reputation: 213203
You need to access the static methods on the class name:
TestoMessaggio t1 = TestoMessaggio.creaTestoMessaggio();
The code that you used:
TestoMessaggio t1 = creaTestoMessaggio();
is equivalent to:
TestoMessaggio t1 = Esempio3_5.creaTestoMessaggio();
since, you are using it in static
context. Now, clearly you don't have that method in Esempio3_5
class, so it fails.
Similarly, change the other line to:
Messaggio.stampaMessaggio(mex);
But IMO, you should override toString
method in Messaggio
instead of providing a static stampaMessaggio()
method.
Upvotes: 2