0

ok, so I have two classes:
Ticket and passenger
passenger class has 3 data members, and one the the ticket data members is:

private Passenger psmgr;

what I want to do is to access passenger's data member using the psmgr variable
how do I do that?

2
Contributors
3
Replies
5
Views
4 Years
Discussion Span
Last Post by OsaidAz
0

let me show you the data members first so it will be more clear:

public class Passenger {

   private String fName;
   private String iName;
   private int ID;

--------

public class Ticket {
    static int NumFirstClass=0;
    static int NumBusinessClass=0;
    static int NumEconomyClass=0;
    static int TicketCounter=23730221;
    private int TicketNumber;
    private Passenger psmgr;

and this is how I tried to do it but it gives me an err:

Ticket x=new Ticket();
        x.psmgr.setID(333);

the err says: psmgr has private access in ticket

1

You need public accessor methods such as getName, getID that simply return the values of those private variables. That way you allow other classes to get the values in a controlled way, but not to change or corrupt them.

Similarly you can create public setID etc methods if you want other classes to be able to change some of those values.
In your case, where presumably every Passenger has an ID and a name, which never change, you should consider having a public constructor that requires those values as parameters to create the Passenger object, and not having any set methods.

0

thank you so much, you just saved my life ... seriously

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.