This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes HFJ doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HFJ doubt" Watch "HFJ doubt" New topic
Author

HFJ doubt

srikanth mycherla
Greenhorn

Joined: Oct 31, 2008
Posts: 26
Dear Ranchers


In head first java of pg 238 and 239 i have a doubt ..

in pg 238 there is a statement ->" no matter where the object reference variable is declared (inside a method vs as as instance variable of a class ) the object always always always goes on the heap ..

Here i understood that the object always goes on heap even when object reference variable is declared inside a method ..(main method usually )

Iam unable to understand this line --Object reference variable is declared as an instance variable of a class ..

1)I have seen like object reference variable is always declared inside main method
please let me know such situations where object reference variable is declared as an instance member of a class or even in any normal method (not in a main method ) If possible please write small code ranchers ..


2)In 239 pg also i am unable to understand this line --what if the instance variable are objects?
and when the new object has instance variables that are object references rather than primitives ?

there is one example on this pg

public class cellphone {
private Antenna ant = new Antenna ();}

my opinion is this example should look like this i suppose

public class cellphone
{
public static void main(String args[])
{
private Antenna ant =new Antenna();}



If iam right then since ant reference variable is declared inside main method then it should be on stack pointing towards heaps object

correct me ranchers




3) IF the instance variable is a reference to an object both the reference and the object it refers are on the heap ...source from HFJ 247 BULLET POINT(2)

How come an instance variable be a reference to an object

I suppose we usually create references inside main method ..correct me

Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816

srikanth mycherla wrote:

Iam unable to understand this line --Object reference variable is declared as an instance variable of a class ..

1)I have seen like object reference variable is always declared inside main method
please let me know such situations where object reference variable is declared as an instance member of a class or even in any normal method (not in a main method ) If possible please write small code ranchers ..


It's just as you would do in a main method, no difference.



You can use an object of any type you want here instead of Object. Or even you may initialize the object reference in the same line as your declaration.




SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3816


2)In 239 pg also i am unable to understand this line --what if the instance variable are objects?
and when the new object has instance variables that are object references rather than primitives ?

there is one example on this pg

public class cellphone {
private Antenna ant = new Antenna ();}



Your statement is not clear to me but this represents a class with an instance reference variable which is pointing to an object of the class Antenna.

I suppose we usually create references inside main method ..correct me


That's just a one place where references can go in. But it's perfectly legal (and main is just another method with a few exceptions from other instance/class methods) to have that in any other method or as a instance variable as in my previous reply.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


In head first java of pg 238 and 239 i have a doubt ..

in pg 238 there is a statement ->" no matter where the object reference variable is declared (inside a method vs as as instance variable of a class ) the object always always always goes on the heap ..

Here i understood that the object always goes on heap even when object reference variable is declared inside a method ..(main method usually )


Here you are absolutely right.

Iam unable to understand this line --Object reference variable is declared as an instance variable of a class ..

1)I have seen like object reference variable is always declared inside main method
please let me know such situations where object reference variable is declared as an instance member of a class or even in any normal method (not in a main method ) If possible please write small code ranchers ..





2)In 239 pg also i am unable to understand this line --what if the instance variable are objects?
and when the new object has instance variables that are object references rather than primitives ?


I have shown in my code, Person object has an instance variable "name", that is a String object.
And another instance variable is "age", that is primitive.

there is one example on this pg

public class cellphone {
private Antenna ant = new Antenna ();}

my opinion is this example should look like this i suppose

public class cellphone
{
public static void main(String args[])
{
private Antenna ant =new Antenna();}



No you are wrong here.

This is the right way.


3) IF the instance variable is a reference to an object both the reference and the object it refers are on the heap ...source from HFJ 247 BULLET POINT(2)

How come an instance variable be a reference to an object
I suppose we usually create references inside main method ..correct me


I think this doubt must have been cleared now by given code.

SCJP 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37897
    
  22
Punit Singh wrote:
. . .

This is the right way.
A very helpful post. Thank you. What you have written is all right, but I think, personally, I would prefer to instantiate the ant object in a constructor.And please use correct indentation, and start your class names with CapitalLetters, please.
srikanth mycherla
Greenhorn

Joined: Oct 31, 2008
Posts: 26
Thanks to punit and vijithakumara..its clear to me now..excellent explanations
srikanth mycherla
Greenhorn

Joined: Oct 31, 2008
Posts: 26





This is posted by Kshitij dogra ..it is also a good answer to this question
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HFJ doubt
 
Similar Threads
Reference Store
how to distinguish the stack and the heap
difference between reference variable and instance variable
stack memory
Array Objects