1

I always use constuctor only for attributes of one object. But i thin when i wrote this :

  public Predmet(int esp,int obaveze,int cena){
    this.cena=cena;
    this.obaveze=obaveze;
    this.esp=esp;

    List j = new ArrayList();
    j.add(8);
    this.nesto=(int) j.get(0);

}

where are stored this ArrayList,does it part of object,or is on stack and have reference to array in heap?

J.P
  • 141
  • 3
  • 9
  • I doubt it matters since you lose reference to `j` as soon as the object has finished constructing. – Makoto Aug 25 '16 at 17:58
  • 2
    The `ArrayList` is entirely unnecessary. Those three lines of code can just be replaced with: `this.nesto = 8;` – David Aug 25 '16 at 17:59
  • As soon as the execution exits the current scope (read "curly brackets") all variables only referenced in that scope are eligible for GC. So the answer is: **it's not stored** (not for long anyway). – Boris the Spider Aug 25 '16 at 18:00
  • 1
    Objects are stored in heap, and removed on gc if it don't have any reference. But your testing program would never need a gc due to not much memory is used, so it will be there till your jvm exit. – Eric Aug 25 '16 at 18:01
  • 1
    @EricWang the overall memory usage is hard to tell from this snippet. – Puce Aug 25 '16 at 18:07

3 Answers3

2

The ArrayList is created on the heap and only referenced by the local variable j from the stack here. After the execution of the constructor it will be eligible for garbage collection.

Puce
  • 37,247
  • 13
  • 80
  • 152
0

Constructor is working very similar the way methods work. At runtime, anything you define inside constructor/method are called local variables. Their scope will end as soon as execution hits the end of the constructor/method. After consturctor, your list will be eligible for GC. However, this.nesto will still get value 8 as its primitive type.

Darpan27
  • 239
  • 1
  • 4
  • 9
0

First, please don't use single variable names! This is bad practice and makes files harder to read. Using a more descriptive name helps to promote code readability.

The way that List j is created, it only exists in the scope of the constructor. After your constructor, List j is no longer accessible. If you wanted it to be accessible after the constructor, have a field for the object. For example:

public class Example {
    private int specialNumber;
    private List<Integer> numberList;
    /**
    * Constructs a new Example Object
    */
    public Example(int exampleNum){
        // specialNumber can be accessed from a Getter method (getSpecialNumber)
        this.specialNumber = exampleNum;
        this.numberList = new ArrayList<Integer>();
        this.numberList.add(exampleNum);
        // numberList is a field of this Example now
        List<Integer> disappearingList = new ArrayList<Integer>();
        disappearingList.add(exampleNum);
        // After this method finishes, disappearingList will be gone
    }
    // disappearingList is no longer accessible
    /**
    * Gets this Example's specialNumber value
    * @return int this.specialNumber
    */
    public int getSpecialNumber(){
        return this.specialNumber;
    }
    /**
    * Gets this Example's numberList
    * @return List<Integer> this.numberList
    */
    public List<Integer> getNumberList(){
        return this.numberList;
    }
}

There is probably a way to hook into some of the Java cleaning methods and pull it out, but that will get a little messy. If you want to be able to create an Object inside another Object, and use it after the constructor, it must be saved as a field.

Brydenr
  • 798
  • 1
  • 19
  • 30
  • Tnx,but i have one question, l i deleted by GC ,but if i have numberList=disapearingList ? – J.P Aug 25 '16 at 18:58
  • If numberList = disappearingList, then the value of disappearingList is saved to numberList, because of how Java stores Objects (http://stackoverflow.com/questions/12740657/how-does-java-saving-object-reference-work) – Brydenr Aug 25 '16 at 19:00
  • if this numberList have reference to the disapearingList ,that list will still exist with refernce in numberList? – J.P Aug 25 '16 at 19:02
  • Yes. Trying to use this.disappearingList will not work however. The data of disappearingList essentially overwrites the data in numberList. (Even though what is really happening is the pointer for the data numberList is moved to point at the data in disappearingList) [pointers in Java](http://stackoverflow.com/questions/1750106/how-can-i-use-pointers-in-java) – Brydenr Aug 25 '16 at 19:04
  • disappearingList does disappear. However, the data stored in disappearingList is actually a chunk of memory. If you set numberList = disappearingList, what you are really doing is saying "Here is some data that is assigned to a variable named disappearingList. Here is some other data assigned to a variable numberList. Make numberList use the same data in memory as disappearingList." Nothing uses the original data assigned to numberList, so the java GC cleans it up. The variable disappearingList also disappears, but its data does not, because numberList now uses that data. – Brydenr Aug 25 '16 at 19:09