• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing Objects in Java  RSS feed

 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know how is an object passed in java in methods.While referring an Java book(p.169) it tells about the difference of how an variable and object is passed in a method but I couldn't get how the passing of object takes place in java.

Here is the code where I am confused on passing objects in java:



Since stu and s are entirely different objects of java it's definite that stu and s would have it's own instance variables isn't it?.So how could a change in s instance variables makes a change in stu instance variables(name)?.Could anyone help me.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Johns wrote:Since stu and s are entirely different objects of java (...)

This assumption is false.
They are different variables that reference the same object.
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thing to note is that Java is always passing by value. The second thing to note is that you always (except primitive types) work with object references, not objects themselves. So in the second example, you are passing by value the reference to the actual Student object. The reference is a local variable in the method body, not an object. And using that handle you can change object's state (ie. member fields).
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomas Linhart wrote:First thing to note is that Java is always passing by value.


Wow that link was very interesting to read but still a doubt persist.Doesn't the reference actually holds the memory location of the object in java?Could you tell me then how java references an object.Also does the java compiler automatically creates the java instance variables for an object when the new keyword is executed during object creation.Could you help me.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Johns wrote:Doesn't the reference actually holds the memory location of the object in java?

Maybe - and it's probably a reasonable way of thinking about it.

Could you tell me then how java references an object.

Not without trawling through the JVM spec (and maybe not even then).

The fact is that Java is a memory-managed language, so the business of memory is up to the JVM. It's perfectly possible that a Java object isn't even kept in one place or in one "chunk" (although I suspect it generally is).

All you need to know is that a reference "points to" an object. It is not the object itself.

Also does the java compiler automatically creates the java instance variables for an object when the new keyword is executed during object creation.

Yes - or rather, the JVM does. But don't forget that those variables, if they point to objects, are simply references; and they will start out life as null. If you also need to create the objects they point to, then you have to do that with an assignment - either directly, or in a constructor.

HIH

Winston
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:If you also need to create the objects they point to, then you have to do that with an assignment - either directly, or in a constructor.


Do you mean to say that they aren't initialized with their default values when the new keyword is invoked by the compiler?Also does that mean they are initialized with null values?Did you meant to say objects in general because I think that the references(object) hold the members and it's values isn't it?For example if there is an object stud of class student the stud contains name,age etc and it's details.Could you tell why did you mentioned about creating an object because I think there's only need of assigning values to the data members through an constructor or directly as you have stated?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Johns wrote:Do you mean to say that they aren't initialized with their default values when the new keyword is invoked by the compiler?

No. The "default value" for a reference just happens to be null.

Did you meant to say objects in general because I think that the references(object) hold the members and it's values isn't it?

No. As I've tried to explain, a reference is NOT an object; it's a "pointer" to an object. Therefore, it doesn't hold any members or values; it simply tells the JVM where to find the object.

For example if there is an object stud of class student the stud contains name,age etc and it's details.

Sure. And those "details" (certainly the name, which is probably a String) may well be objects themselves.

Could you tell why did you mentioned about creating an object because I think there's only need of assigning values to the data members through an constructor or directly as you have stated?

Simply to highlight the fact that you have to assign them. If you create a Student with:

  Student s = new Student();

it's highly likely that when you try to print out s.name, you will get

  null

because you haven't initialized the name.

  Student s = new Student("Joe Bloggs", 18);

is something quite different though.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Johns wrote: . . . tell me then how java references an object.
No. You can look through the Java® Virtual Machine specification and I think you will find it doesn't tell you. Which means that different implementations can do it differently from each other. In fact it may change with the number of references; maybe a large application with millions of objects does it differently from a small application with dozens of objects.

As Winston has hinted, this is something you do not need to know.
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
No. The "default value" for a reference just happens to be null.


Thanks.Does java compiler allocate memory for the object or whether it allocates just for the instance variables during object creation?Could you tell me whether java compiler allocates memory for an object when an object is created using the new keyword or it allocates memory only after the user assigns values to the instance variable.Also whether java allocates memory only to the instance variables that user assigns or does it allocate memory for all the instance variables of an object?
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to consult JLS about this topic, which I believe covers all your questions.
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Johns wrote:
Thanks.Does java compiler allocate memory for the object or whether it allocates just for the instance variables during object creation?Could you tell me whether java compiler allocates memory for an object when an object is created using the new keyword or it allocates memory only after the user assigns values to the instance variable.Also whether java allocates memory only to the instance variables that user assigns or does it allocate memory for all the instance variables of an object?


The compiler has nothing to do with memory allocation.
That happens during runtime, and is handled by the JVM.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Johns wrote:Could you tell me whether java compiler allocates memory for an object when an object is created using the new keyword or it allocates memory only after the user assigns values to the instance variable. Also whether java allocates memory only to the instance variables that user assigns or does it allocate memory for all the instance variables of an object?

1. When you use the new keyword (basically).
2. All of 'em.

I think you're still having trouble understanding what a "reference" is. It's a pointer; and I believe, on a 32-bit system, that it takes up 4 bytes - and it'll take up those 4 bytes whether it actually points to an object or it contains null.

The only thing you don't know is what kind of pointer it is - and Java doesn't want you to know, because it's none of your business. Rest assured that it will point to an object though...unless it's null of course.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my brain, I always think of creating an object as building a house, and the varaible/reference as a notecard where I write down the address where the house was built.

I can have a notecard with no address on it. I can look at it, but it's blank, and doesn't do me much good. this would be like creating a reference that doesn't point to an object:



I can talk about 's', use it in my code, but it's blank - or rather, contains a null value.

I can build a house somewhere. Or rather, I can call a contractor, say "Build me a house wherever you can find some property". In java, it's like this:



Now there is a house built...somewhere. But I have no way to find it, because I never wrote down the address. Eventually, the housing commission will decide the house is abandoned, and clear the lot.


What I need to do is write down the address. A common way is to combine the above two lines like this:


this creates the object (on the right side). That returns the reference to the object (the address of the house, so to speak), and then I write it down by saving it in my variable myFoo. Whenever I need to find that house, I use the myFoo reference.

I can then make as many copies of that reference. I could do this:


Now I have two places where the same address is written down - but there is still only one house.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to know how is an object passed in java in methods.


This is an example of how using the wrong words to describe something in programming leads you to confusion.

I cringe when I see "pass an object".

Bill
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These JavaRanch Campfire Stories might also help you to understand reference variables and pass-by-value:

Cup Size -- a story about variables
Pass-by-Value Please (Cup Size continued)
 
Tim Holloway
Saloon Keeper
Posts: 18799
74
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for info. I don't know about the Java implementation spec, but a Reference doesn't have to be a pointer. For example, it can be an index into a table of pointers. Or a chain of tables of pointers, et cetera. This kind of trick is sometimes used to facilitate memory management and garbage collection. The original MacIntosh OS had a scheme like this (handles).

The only thing that really matters is that a reference is something that resolves to a specific instance of an object so that the object may be inspected, modified, or whatever. At the Java source code level, you don't know, cannot know, and shouldn't even care whether it's a vanilla pointer or something more esoteric.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One must keep in mind that a programming language is just an abstraction to the underlying machine instructions of a given computer architecture which simply mean its just a tool...

While using any tool, all one needs to concern him/herself with is its interface to allow proficient usage of that tool... There is no need to understand the implementation details if the objective is just to use and not implement...

Now focusing on your direct question... If you understand the word reference which is to refer to something... You will come to a realization that there is only one entity involved...

Just as you are a single entity which can be referred to by many names that your peers might attach to you which can be [bullethead, handsomeguy, snoopy]... Any one of those name will still refer back to single you the same entity...

The same principle is applied to the programming construct of a reference variable in the Java programming language...
 
Justin Johns
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay.As all of you have stated I'm not going deep into the details of memory allocation in java.Also thanks for the support everyone shown that made me to understand how an object reference is passed by value in java.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Justin Johns wrote: . . . details of memory allocation in java. . . .
Remember those details can change from version to version. In Java5 the memory model changed completely and in Java8 the “permanent generation” of memory was removed. I can't remember the full details.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!