0

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

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
Mick Hardins
  • 25
  • 1
  • 9

3 Answers3

2

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.

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • well i tought that i shouldn't use the (.) since it's a static method... Am i wrong? – Mick Hardins Aug 18 '13 at 20:38
  • @MickHardins. Well, if you are calling static methods of the same class, then you don't need to qualify it with the class name. But if you are invoking the static method of some other class, then you have to use qualified name. – Rohit Jain Aug 18 '13 at 20:39
0

it is not getting the functions from the other classes. try to associate them using dot(.)opertaor like TestoMessaggio t1 = TestoMessaggio.creaTestoMessaggio();

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
0

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
Jason C
  • 38,729
  • 14
  • 126
  • 182