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?

6
  • The references is any way refer to the object in the heap Commented Aug 22, 2017 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? Commented Aug 22, 2017 at 10:43
  • If you have 100 methods, will you create 100 objects to call them? Commented Aug 22, 2017 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. Commented Aug 22, 2017 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 Commented Aug 22, 2017 at 11:27

5 Answers 5

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

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

10 Comments

so object is just a permanant memory location with the data of the class??i want to know what exactly is object?
Yes Exactly @Nalin
You can say Object is like Container, Which contains variables and methods defined in the class
little confused !
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
|
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.

2 Comments

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.
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.
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.

3 Comments

Thanks. i never said demo is not a class.
d is not a reference?
So the human1 will be the reference for the actual object in the heap memory that is an instance of Human?
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(); 

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.