1

I am creating a object (bb) of Box class.and storing 25 to its attribute value. The object will be created and it will have memory.Now I want to create new instance of Box class but i want it to point it to the same object created for bb and not allocating it new memory again.For example if write Box cc new Box(); and print out cc.value it should print 25.It should not allocate new memory.That is every time we create object it should refer to bb .For example Box newOne = new Box(); and newone.value should give us 25.Is this possible? I don't know if i have described the problem well.I came across this type of question recently in an exam.

Box bb = new Box();
bb.value = 25;
Box cc = new Box();//It should not allocate new memory it should refer to 
//the previous memory that we used for bb.
System.out.println(cc.value);//It should give us 25

class Box{
  int value;
}
Mahesh Gupta
  • 55
  • 1
  • 10
  • 1
    It is called `Singleton Pattern`. You can take a look at this question for more information: https://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java – endertunc Oct 14 '17 at 08:54
  • 1
    make the value attribute static (ie: `static int value;`) – davidchoo12 Oct 14 '17 at 08:54

2 Answers2

2

This should answer your question :

Box box = new box(25);
Box box2 = box;

As box2 is not a new object, it will refer as the first one created in the memory.

Monkey
  • 21
  • 1
2

Now I want to create new instance of Box class but I dont want to allocate new memory to it and i want to instatiate the same object created for bb

It makes no sense.
Either you instantiate a new object and it allocates a new object in memory or you reuse the same object by assigning the same object to multiple variables.

1) If you want to create not more than 1 instance of Box, you may use the singleton pattern.

public class Box{

    private Box box = new Box();
    public int value;

    private Box(){
    }

    public static Box of(){
        return box;
    }
}

And use it in this way :

Box bb = Box.of();
bb.value = 25;
Box cc = Box.of();
System.out.println(cc.value);

2) If you want to have a single value field shared among all instances of Box, make this field static :

public class Box{

    public static int value;
  ...
}

And use it in this way :

Box bb = new Box();
bb.value = 25;
Box cc = new Box();
System.out.println(cc.value);

It will not avoid creating multiple instances of Box but it will allocate a single allocation for the int value field.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • You could create different instances which could share the same data. Obviously makes no sense for a single `int` but imagine it's a huge `int[]`. This could be passed in the constructor so two distinct instances would effectively "share memory". – lexicore Oct 14 '17 at 09:15
  • @lexicore Sure it would be valid. If the field is shared among all instances, using static is also an option. – davidxxx Oct 14 '17 at 09:29