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?

Recommended Answers

All 3 Replies

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.