Home > database >  How do I reach an object that created from another class's constructor
How do I reach an object that created from another class's constructor

Time:11-30

I tried to create trip object in Bus class constructor but I can't reach it from main class.

I thought I stored trip object in Trip class but console said it can't find the symbol, then I thought it stored at Bus class but it didn't work either

Main class

public class Interface
{
    public static void main(String args[])
    {       
        Bus bus1 = new Bus(1);
        Trip.trip1.toString();
    }
}
    

Bus class

public class Bus
{
    private int tripNumber;
    private String model;
    private String type;
    private int age;
    private int capacity;
    private int remainingCapacity;
    private boolean[][] seats;
    
    public Bus(int tripNumber)
    {
        this.tripNumber = tripNumber;
        
        if (tripNumber==1)
        {
            this.model = "Setra";
            this.type = "2 2";
            this.age = 8;
            this.capacity = 40;
            this.remainingCapacity = 23;
            
            Trip trip1 = new Trip(this, tripNumber);
        }
    }
    public String toString()
    {
        return ("\n\tBus Information:\n\t\tBus: "   this.model   "\n\t\tType: "   this.type   "\n\t\tAge: "   this.age   "\n\t\tCapacity"   this.capacity   "\n\t\tRemainingCapacity"   this.remainingCapacity);
    }
}

Trip class

public class Trip
{
    private int tripNumber;
    private String date;
    private String origin;
    private String destination;
    private String departureTime;
    private String arrivalTime;
    private Bus assignedBus;
    
    public Trip(Bus bus, int tripNumber)
    {
        if (tripNumber==1)
        {
            this.tripNumber = 1;
            this.assignedBus = bus;
            this.date = "27/11/2022";
            this.origin = "Ankara";
            this.destination = "Istanbul";
            this.departureTime = "00:15";
            this.arrivalTime = "06:30";
        }
    }
    
    public String toString()
    {
        return tripNumber   ") Trip Information: \n\tDate: "   this.date   "\n\tFrom: "   this.origin   " to "   this.destination   "\n\tTrip time: "   this.departureTime   " to "   this.arrivalTime   this.assignedBus.toString();
    }
}

CodePudding user response:

There are a couple of problems in the code.

Bus.java

You have declared the trip1 variable as a local variable that only exists within the constructor, and hence it is not available and cannot be referenced elsewhere. Make is a class variable to make it available to the instances of your class.

Interface.java

You are trying to access the trip1 variable at the class level whereas you should access it on the instance of the Bus class (something like bus1.trip1.toString() it is better to create a getter for this purpose). NOTE: You will need to fix the first issue for this to work.

See the working code here - https://ideone.com/ej8IGD (note that the main class name has changed as required by the platform)

While this will work - I would strongly suggest you to restructure the code a bit to handle cases like handling more than one trip (hint: use a List/Array)

  • Related