How to call the method of one class within another (bluej)

Asked

Viewed 47 times

0

Here’s what I’ve been doing

public void valorConta(){
    int i=0;
    float totalConta=0;
    for(i=0;i<listaligacoes.length;i++){
    totalConta+=(listaligacoes[i].calcularDuracao()*listaligacoes[i].getvalor());
}
}

I have a variable link listing of the Type Link(class) I would like to in it, call the methods calculate duration and value, to set the value of the account in this current class. I use the bluej.

  • Just to be clear, I’m a beginner, there’s a lot of term I don’t understand yet.

1 answer

1

Use Static...

public static void valorConta(){
    int i=0;
    float totalConta=0;
    for(i=0;i<listaligacoes.length;i++){
       totalConta+=(listaligacoes[i].calcularDuracao()*listaligacoes[i].getvalor());
    }
}

To call in another class is simple

NomeDaClasseQueTemOMetodo.valorConta();

Remembering that this way listaligacoes needs to be a variable Static, or you can receive it as Parameter, and the way up there you do not return anything... If you want to return totalConta barter void for int

  • The problem is that if this vector listaligacoes not static it will not work. It would be better if you quote this. Because if this vector belongs to the object, or needs to belong to the object... this solution will not work.

  • That function valorConta could receive this list in the parameter.

  • I thought about this problem tbm. ,.. But I think it runs a little away from the guy’s question, but... I’ll put there in the answer

  • Yes.. runs away a little even. She lacked to detail the question better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.