0

I had an excellent response to an earlier question so I'm going to push my luck here. While there is an excellent post already about this;What does a "Cannot find symbol" compilation error mean? I am stuggleing to find which reason applies and why.

The error here occurs when creating the object FirstClassTicket, both before and after the equals where it calls FirstClass. I assume I have made an error when I inherited FirstClass from Ticket as this was originally running fine calling Ticket before I began to include to different Ticket types.

class Booking{
    public  static   class Ticket{
        int Price;
        int Amount=1;
        int Discount=0;
        public int WhatCost(){
            int Cost =Amount*(Price-Discount);
            return Cost;
        }
        //Inheriting Ticket classes.
    class FirstClass extends Ticket{ //Defines standard class tickets.
        int Price =10;
    }

    }
    public static void main(String args[]) {
      FirstClass FirstClassTicket=new FirstClass(); 
        Scanner scanner = new Scanner(System.in);
        System.out.println("Welcome to the advance booking service.");//Introduce program.
        System.out.println("A first class ticket costs:");
        System.out.println(FirstClassTicket.WhatCost());
    }
}
Community
  • 1
  • 1
J.Blake
  • 29
  • 1
  • 9
  • 2
    You defined FirstClass as an inner class of Booking. Do yourself a favour and define each class in a seperate file as per java standard. Unless of course you actually want to have an inner class ( which i highly doubt). Related reading material: [Java Tutorial: Nested Classes](https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) – OH GOD SPIDERS Apr 26 '17 at 13:39
  • 2
    ...or just make `FirstClass` `static`. – Andy Turner Apr 26 '17 at 13:39
  • Okay, I'll take a look at that. Also attempting to convert to `static class FirstClass extends Ticket{ //Defines standard class tickets.` didn't change the errors. – J.Blake Apr 26 '17 at 13:44
  • Okay so moving the {}'s and making it static solved the problem. Thank you both. – J.Blake Apr 26 '17 at 13:51
  • Actually these classes should not be nested. Did you understand _why_ making it static worked? – Lew Bloch Apr 26 '17 at 16:37

1 Answers1

0

You have defined FirstClass as inner class of ticket. Make the FirstClass as static and access it as shown below.

Booking.Ticket.FirstClass FirstClassTicket=new Booking.Ticket.FirstClass();
Zak
  • 114
  • 3