privatename
privatename

Reputation: 145

Java cannot find symbol when calling a method from another class

Im trying to call the method show in the klub class, from my demo class, and to call the method visAlleAtleter in the demo class (from my klub class), but I get an error saying it can't find the symbol and then the name of the method. I have tried to specify where I want it from, but I'm not certain how to do it. This is my Klub class

import java.util.ArrayList;

public class Klub {
    
    private String navn;
    private ArrayList<Atlet> atleter;

    public Klub(String navn) {
       this.navn = navn;
       atleter = new ArrayList<>();
    }
    
    public void addAtlet(Atlet atlet) {
        atleter.add(atlet);
    }
    
    public void visAlleAtleter() {
        System.out.println(navn);
        for(Atlet atlet: atleter) {
            atlet.display();
            
        }
        
    }
    public void visAlleAtleter2(boolean samletValue) {
      if(samletValue = true) {
          Atlet.show();
        } else {
            
        }
    }
}

this is my Demo class

public class Demo { 
    public static void main(String[] args) {
        
        Klub minKlub = new Klub("SWU United");
       

        Atlet atlet1 = new Atlet("Pan", "Tennis", 43.5, 21);
        Atlet atlet2 = new Atlet("Peter", "Golf", 41.5, 29);
        Atlet atlet3 = new Atlet("Robby", "Tennis", 43.5, 20);
        Atlet atlet4 = new Atlet("Niklas Klein", "Maisball", 21.2, 23);
        Atlet atlet5 = new Atlet("Linni Maister", "Maisball", 44.1, 32);
        Atlet atlet6 = new Atlet("Dennis Michael", "Maisball", 32.5, 11);
        minKlub.addAtlet(atlet1);
        minKlub.addAtlet(atlet2);
        minKlub.addAtlet(atlet3);
        minKlub.addAtlet(atlet4);
        minKlub.addAtlet(atlet5);
        minKlub.addAtlet(atlet6);
   
         Klub.visAlleAtleter();

    
    
    } 
    
}

and my atlet class

public class Atlet {

    private String navn;
    private String sportsgren;
    private double pris;
    private int alder;

    public Atlet(String navn, String sportsgren, double pris, int alder) {
    
    this.navn = navn;
    this.sportsgren = sportsgren;
    this.pris = pris;
    this.alder = alder;
    
    }
    
    public void oppdaterPris(double nyPris) {
        pris = nyPris;
    }
    
    public double getPris() {
        return pris;
    }
    
    public double predSalgspris() {
        return pris-0.95*Math.abs(25-alder);
    }
    
    public void display() {
        System.out.println(navn + " (" + alder + ") - " + 
        sportsgren + ": " + pris + "kr (" + predSalgspris() + "kr)");
    }
    
}

Upvotes: 1

Views: 1397

Answers (2)

Snusifer
Snusifer

Reputation: 553

The problem lies in the line Klub.visAlleAtleter(); in the main method in Demo. To correct this you have to replace it with minKlub.visAlleAtleter();.

The reason for this is that according to your code, you have an instance of the class Klub called minKlub. The method visAlleAtleter() is bound to any instance of the class Klub, not the class Klub itself.

EDIT: To illustrate why your attempt could be an issue, consider this example:

public class Demo { 
    public static void main(String[] args) {
        
        Klub minKlub = new Klub("SWU United");
        Klub dinKlub = new Klub("SEU United")
       
        Atlet atlet1 = new Atlet("Pan", "Tennis", 43.5, 21);
        Atlet atlet2 = new Atlet("Peter", "Golf", 41.5, 29);
        minKlub.addAtlet(atlet1);
        minKlub.addAtlet(atlet2);

        Atlet atlet3 = new Atlet("Mads", "Fotball", 123, 123);
        Atlet atlet4 = new Atlet("Mikkelsen", "Curling", 123, 123);
        dinKlub.addAtlet(atlet3);
        dinKlub.addAtlet(atlet4);
   
        Klub.visAlleAtleter();
    }    
}

In this example, when calling Klub.visAlleAtleter(), it is impossible to know whether Java should call the method on minKlubb or dinKlubb. Its important that you see minKlub and dinKlub as physical instances of the Klub class.

Coincidentally, writing Klub.someArbitraryMethod() is actually a commonly used feature in java, where someArbitraryMethod() is statically declared. This way of writing is only possible if you declare the method as static:

public static void someArbitraryMethod() {...}

This means that the method doesn't belong to any physical instances of the class Klub, but rather it belongs to the class Klub itself. Doing

minKlubb.someArbitraryMethod();

will therefor cause a compilation error.

Upvotes: 1

Egor
Egor

Reputation: 1409

Method visAlleAtleter is not static so your can't call it as you do Klub.visAlleAtleter(). Just call method on object minKlub.visAlleAtleter().

Upvotes: 2

Related Questions