0
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?

Assafs
  • 3,257
  • 4
  • 26
  • 39
Nalin
  • 3
  • 4
  • The references is any way refer to the object in the heap – sForSujit Aug 22 '17 at 10:43
  • What do you mean by "directly use"? How else would Java know which object you're referring to? Can you post some code for how *you think* this should look? – Bernhard Barker Aug 22 '17 at 10:43
  • If you have 100 methods, will you create 100 objects to call them? – sForSujit Aug 22 '17 at 10:45
  • This could go in a broad discussion, my recommendation to you is to dig in for the basics because this is where the foundation of object oriented programming lives. There is lot of stuff out there with verity of information. [Start from here](https://www.javatpoint.com/java-oops-concepts). – Rohit416 Aug 22 '17 at 10:52
  • See i know that object is an instance of a class and class is the blueprint from which we create the object but exactly what is an object?? i mean that what is the purpose of the object @Dukeling – Nalin Aug 22 '17 at 11:27
  • Exactly my question please expain that @sForSujit – Nalin Aug 22 '17 at 11:29

5 Answers5

3

I will try to explain as better as possible to me. So look at the below Image :

enter image description here

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

sForSujit
  • 987
  • 1
  • 10
  • 24
  • so object is just a permanant memory location with the data of the class??i want to know what exactly is object? – Nalin Aug 23 '17 at 09:38
  • Yes Exactly @Nalin – sForSujit Aug 23 '17 at 09:39
  • You can say Object is like Container, Which contains variables and methods defined in the class – sForSujit Aug 23 '17 at 09:40
  • little confused ! – Nalin Aug 23 '17 at 09:40
  • OHHHHK so to access those methods in stack we use the reference that helps us to access all the methods and variables(instsance) stored in heap – Nalin Aug 23 '17 at 09:42
  • in your code, d will be at Stack, and new Demo() will create object in the heap, and someFunction() will be in that Object in heap – sForSujit Aug 23 '17 at 09:42
  • I would like to add some more, You use reference to call those methods in the heap, But we can call the methods using new Demo().somefunction(), But this is not good practice – sForSujit Aug 23 '17 at 09:45
  • So to easily access those methods instead of creating seperate objects with seperate copies of those methods we directly create one store the data in that and use the reference to access all the methods – Nalin Aug 23 '17 at 09:45
  • @Nalin yes, you understand it very well now – sForSujit Aug 23 '17 at 09:46
  • On the one hand this answer is "true", but on the other hand ... it leaves out so many important aspects of OOP in general. To a certain degree ... feels almost awkward to read this. – GhostCat Aug 23 '17 at 17:45
2

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:

  • an object
  • or a class itself

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    I'd say that in case of Java this is more about namespaces. If you tilt your head enough, then even `Demo d = new Demo();` creates its own namespace called something like `package.Class.method.vars.d`, and then calling methods on `d` will call code in context of that namespace. – M. Prokhorov Aug 22 '17 at 11:22
  • Yes, in deed "head-tilting", but still an interesting point of view! Like it! **But**: your suggestion there breaks when we think about *polymorphism*. You can have `d.foo()` - and it is not at all clear which method will be called when `foo()` is overridden in different classes. – GhostCat Aug 22 '17 at 11:23
2

I try to explain the basics because I think you messed up the names a bit^^

  • Demo is a class not an object
  • d is an object from the class Demo

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.

0

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();
Emax
  • 1,343
  • 2
  • 19
  • 30
0

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.