0

I have a static arrayList used elsewhere in my code called jobsList. I would like to add elements to it using a static method. For some reason it is overwriting the jobList so that every time I add an element, the 1st element is the element I just added and nothing else. As in, no other elements are ever added. Relevant code below:

public static ArrayList<Job> jobList = null;

public JobSchedule() {
    jobList = new ArrayList<Job>(10);

}

public static Job addJob(int time) {
    System.out.println("Adding job " + time);
    Job j = new Job(time);

    jobList.add(j);
    System.out.println("Current joblist size: " + jobList.size());
    System.out.println("First element: " + jobList.get(0).weight);
    return j;
}

The output from the printlines looks like this:

Adding job 8
Current joblist size: 1
First element: 8
Adding job 5
Current joblist size: 1
First element: 5

Ideally each time I add it should increment the size and put the job at the correct index, so I'm not sure why the arraylist is being overwritten.

7
  • answer is because its static. remove every static and it works Commented Nov 15, 2016 at 7:24
  • static ArrayList<Job> jobList.... public JobSchedule() { jobList = new ArrayList<Job>(10); }, something looks not right here. Commented Nov 15, 2016 at 7:25
  • Do you mean the method or the arrayList? I need the arrayList to remain static and when i remove static from the method declaration I still get the same problem. Commented Nov 15, 2016 at 7:26
  • 1
    How are you calling this method? Commented Nov 15, 2016 at 7:26
  • 1
    In between consecutive addJob call are you calling new JobScheduler();? Commented Nov 15, 2016 at 7:29

5 Answers 5

3

I think you're calling the constructor every time,

JobSchedule(){
    jobList = new ArrayList<Job>(10);
}

This creates a new ArrayList every time you call it.

No need for the constructor. Do something like this:

public static ArrayList<Job> jobList = null;

public static Job addJob(int time) {

    if (jobList == null) {
        jobList = new ArrayList<Job>(10);
    }
    System.out.println("Adding job " + time);
    Job j = new Job(time);

    jobList.add(j);
    System.out.println("Current joblist size: " + jobList.size());
    System.out.println("First element: " + jobList.get(0).weight);
    return j;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I'm not calling the constructor from within the body of the addJob. I also need the constructor to actually create the (graph-like) job schedule so I can do other things from the schedule class, like sort.
No need to do anything in the constructor. Apart from anything else, the code here is not thread safe. Assign the list when it is declared.
if you are calling the constructor you are initializing list every time and if you are not calling constructor you are putting values in null list which is more worse
1

In your current code, when you invoke your constructor new JobSchedule(), a brand new ArrayList object will be created and assigning to the same reference.

Infact, I would suggest you to mark your jobList reference variable with private, static and final as shown below:

private static final ArrayList<Job> jobList = new ArrayList<Job>(10);
public JobSchedule() {
}
public static Job addJob(int time) {
  System.out.println("Adding job " + time);
  Job j = new Job(time);

  jobList.add(j);
  System.out.println("Current joblist size: " + jobList.size());
  System.out.println("First element: " + jobList.get(0).weight);
  return j;
}

private - can be accessed within the same class

static - maintained a single copy for the whole class

final - the reference can't be reassigned (also can't be inherited) with a new arraylist

It is not recommended to expose your whole jobList (arraylist) as a public variable, rather try to get a specific Job using some getJob(T t) method (so is the reason I have marked it as private).

Comments

0

The most likely reason is that before calling addJob(5), you're calling the constructor JobSchedule(), which is overriding the static list.

To address this, avoid initializing static variables in instance methods

public static ArrayList<Job> jobList = new ArrayList<Job>(10);

public JobSchedule() {
}

And the code should work as expected. Alternatively, make both the jobList field and the addJob method non-static. You'd also have to NOT call the constructor each time before calling addJob

Comments

0

Thanks for the help. I ended up rewritting my code so that it takes an empty constructor, and making a subclass just a normal nested class, passing in an instance of the super class in the nestedclass object and then being able to call sort() on that.

public JobSchedule() {}

public Job addJob(int time) {
    Job j = new Job(time, this);
    jobList.add(j);
    return j;
}

Nested class...{
public JobSchedule schedule = null;

    private Job(int time, JobSchedule schedule)
    {
        this.schedule = schedule;
        this.weight = time;
    }

    public int getStartTime() {
        schedule.kahnSort();
        .
        .
        .
}

Comments

0

I think you add element every time before your new a JobSchedule object. Do not instantize the static ArrayList in the constructor

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.