user725913
user725913

Reputation:

Variable to store data, that can be accessed by any class

I have created a class called aptSchedule which, like its name suggests, keeps track of appointments in the form of a schedule. I would like to create a method that looks for and finds the first open appointment. In order to create this method I must first figure out a way to find all appointments. I figure that I will need to create a public variable of sorts, but I am not very familiar with Java and I was curious where and how should I create such a variable? Am I explaining myself well enough?

Thank you, Evan

Upvotes: 1

Views: 736

Answers (2)

Attila
Attila

Reputation: 28762

You will have to store all the appointments in some sort of collection, let's go with a simple List<Appointment> (assuming each appointment is stored in a(n objec of type) Appointment). Then you can have your class like this:

public class aptScheduler
{
  List<Appointment> appts = new LinkedList<Appointment>();

  public aptScheduler()
  {
    // constructor
  }

  public Appointment findAppointment()
  {
    // search for appointment in appts, and return the first suitable one
  }
}

Upvotes: 1

csaunders
csaunders

Reputation: 1742

I guess you're looking for a Singleton.

public class MySingleton {
  private static MySingleton _instance
  private MySingleton();
  public static getInstance() {
    if(_instance == null) { _instance = new MySingleton(); }
    return _instance;
  }

  // Add your scheduling methods here I guess
}

Bear in mind that while singletons do the job but they can add some pretty tight coupling to your code.

Edit: I may have misinterpreted your question incorrectly. This is if you are looking to have your variable globally accessible.

Upvotes: 0

Related Questions