• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HFJ doubt

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to punit and vijithakumara..its clear to me now..excellent explanations
 
srikanth mycherla
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





This is posted by Kshitij dogra ..it is also a good answer to this question
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic