Demo d = new Demo();
d.someFunction();//Why are we using the reference?
Why do we exactly need the reference? Why cant we directly use objects?
Demo d = new Demo();
d.someFunction();//Why are we using the reference?
Why do we exactly need the reference? Why cant we directly use objects?
I will try to explain as better as possible to me. So look at the below Image :
When you create an Object using Demo d = new Demo(), d
will be placed into the Stack which is a temporary memory location, And new Demo() means it will create an Object on Heap which is a permanent memory location.And d will start referring to the Demo objects in heap.
So now according to your question, Why we use references?
1) If you don't use references and suppose you have 100 methods in your class and you need to call those methods. So you will need to create 100 Objects of the same type which will need more memory which is totally a not good practice to code.
new ClassName().method1();
new ClassName().method2();
new ClassName().method3(); and so on....There will be much wastage of memory
2) But if we use references, We will need only 1 Object to call any method of the class. So the use of references is more efficient.
d.method1();
d.method1();
d.method1();
So I would say, We don't need references, and we can use Objects directly, But in that situation, we will unnecessarily waste the memory.
I hope this will help. Let me know if you still find any doubt. Thanks
Java does not have a concept like free functions. In java, any piece of code that you want to call from another class can only be "accessed" using certain context.
This context can either be:
In other words: you have to call methods. And those are either static (like Arrays.asList()
for example) or non-static - and then they need to be invoked "on" some object (like System.out.println()
where println()
is invoked on an object of the PrintStream
class).
And please note: the "reference" doesn't do "any work" for you. It simply represents the context to which the method you intend to invoke is "bound" to.
I try to explain the basics because I think you messed up the names a bit^^
The best way to understand that is with an example:
Let´s say you have the class Human. What are things that a human has? A name, prename, skin color, country, ... Let´s say we use those things in form of variables on our class human.
Now we create a human object and we call him John that would look like this:
Human human1 = new Human();
human1.name = "John";
But John is pretty lonely so we create another human object called Sarah.
Human human2 = new Human();
human2.name = "Sarah";
I hope that I could help you.
Because so you can call multiple methods of the same object
Demo d = new Demo();
int anotherValue = d.someAddFunction(5, 5);
d.someFunction();
Or let's say you have to pass the object reference to another class
Demo d1 = new Demo();
Demo d2 = new Demo();
DemoCombiner.combineDemo(d1, d2);
d1.someFunction();
To cut a long story short: you can't LABEL allocated memory on the dynamic heap. In static stack you can. NAME vs ADDRESS.
S T A C K A L L O C A T I O N (NAME)
Int x = 4 -> you actually do:
1 Allocate memory on stack with size of int.
2 Give this memory location NAME 'x'.
3 Put value 4 in x.
H E A P A L L O C A T I O N (ADDRESS)
Student Student1 = new Student('Peter') -> you actually do:
1 Allocate memory on Heap with size of object Student.
(You can't assign a name to this location on the heap)
2 Copy the the object's location ADDRESS into reference Student1.
3 Put value 'Peter' in the objects field
So if you can't access a location by NAME you can do it by ADDRESS.