1

As I said in theme, but when I am trying to set a new object, the old ones looks same as the last one:

 public class Test {

private static List<Booking> list = new ArrayList<Booking>();
private static List<Booking> lista = new ArrayList<Booking>();
public static void main(String[] args) 
{
    Booking[] book=new Booking[3];
    book[0]=new Booking();
    book[1]=new Booking();
    book[2]=new Booking();
    list.add(0,book[0]);
    list.add(1,book[1]);
    list.add(2,book[2]);
    list.get(0).setYear("1900");
    list.get(0).setMonth("january");
    list.get(1).setMonth("september");
    list.get(1).setYear("1600");
    list.get(2).setYear("2000");
    list.get(2).setMonth("may");
    System.out.println(list.get(0).getBook());
    System.out.println(list.get(1).getBook());
    System.out.println(list.get(2).getBook());
}

 }

And the booking class:

  public class Booking 
  {

public static String getMark()
{
    return m;
}
public static String getWym()
{
    return w;
}
public static String getWyw()
{
    return wyw;
}
public static String getImie()
{
    return imie;
}
public static String getNaz()
{
    return nazwis;
}
public static String getNr()
{
    return nr;
}
public void setMark(String m)
{
    this.m=m;
}
public void setWym(String w)
{
    this.w=w;
}
public void setWyw(String wyw)
{
    this.wyw=wyw;
}
public void setImie(String imie)
{
    this.imie=imie;
}
public void setNaz(String nazwis)
{
    this.nazwis=nazwis;
}
public void setNr(String nr)
{
    this.nr=nr;
}
public void setYear(String year)
{
    this.year=year;
}
public void setMonth(String month)
{
    this.month=month;
}
public void setDay(String day)
{
    this.day=day;
}
public void setHour(String hour)
{
    this.hour=hour;
}
public static String getYear()
{
    return year;
}

public static String getMonth()
{
    return month;
}
public static String getDay()
{
    return day;
}
public static String getHour()
{
    return hour;
}

public String getBook()
{

    String Book=getYear()+getMonth()+getDay()+getHour()+getMark()+getWym()+getWyw()+getImie()+getNaz()+getNr();
    return Book;

}
private static String year;
private static String month;
private static String day;
private static String hour;
private static String m;
private static String w;
private static String wyw;
private static String imie;
private static String nazwis;
private static String nr;

}

Here is what I got after compilation:

2000maynullnullnullnullnullnullnullnull

2000maynullnullnullnullnullnullnullnull

2000maynullnullnullnullnullnullnullnull

Instead of: 1900januarynullnullnullnullnullnullnullnull

1600septembernullnullnullnullnullnullnullnull

2000maynullnullnullnullnullnullnullnull

1
  • Why don't you use a constructor for Booking class? Commented Nov 25, 2016 at 11:09

2 Answers 2

4

remove the static from your all your fields in Booking

Sign up to request clarification or add additional context in comments.

Comments

2

You are using static variables inside your Booking class and because of this all instances have common fields year, month, etc...

Making field static is like attaching it to whole class context not instance.

The effect is that the last modification (setting may 2000) is modifying all instances in result

You should make these fields non static and then instances will have their own values

class Booking {
    private String year;
    private String month;
    private String day;
    //another fields...

    public String getYear() {
        return this.year;
    }
    //another methods...
}

Also take a look at reference to understand usage of static keyword. You definitely abuse it.

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.