• 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

Trying to access the instance variable of Class Bar from Class Foo --------- why error ?

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



1) In K&B chp 3 (page- 218) the topic of shadowing the object reference .

I would like to know how we can access the instance variable of class Bar from Class Foo.

2 )One query Class Bar object is created outside the main method with reference var b.

And Class Foo object is created inside the main method with reference var f.

Both the objects are created inside the Class Foo.

So we can create objects anywhere inside the class.

Since i usually create the objects inside the main method so i found this little strange.

Can anyone explain me both 1 & 2 doubts.

Thank you.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a capitalization problem, at one place you've written barNum and barnum at the other place...
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:There's a capitalization problem, at one place you've written barNum and barnum at the other place...



But again my question is:

i mean to access the instance variable of another class syntax goes with the ref var of both the classes:

i.e refVar Bar. refVar Foo. inst var name

Correct.

Secondly i can create the objects anywhere inside the class no matter outside or inside the main method.

Thank you.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the reference variable of other class is the instance variable of the class in which you want to use it then you need the reference variable of both classes like you did in your code but is the reference variable of other class is in a method then you dont need the reference varaible of class you are using it in.
for example I have modified your code a little:


yes you can create an object anywhere in the class whether as an instance variable, in main method or in any other method of class.

Example for declaring in any other method:


 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means i can have one instance variable which belongs to both the class.

In any case if i want to use the inst variable from any one of the class then i have two the reference var of both the classes.

it goes this way:



Correct it goes this way.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

dimple bav wrote:That means i can have one instance variable which belongs to both the class.


No, you have an Instance variable 'b' which belongs to class Foo and refers to an object of class Bar.

dimple bav wrote:In any case if i want to use the inst variable from any one of the class then i have two the reference var of both the classes.



If you access a reference variable declared in your class as instance variable then only you need two reference variables.
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Neha,

It goes this way num is an instance var of Class Bar.

Now in class Foo

I have 2 objects created one of class Foo itself and other of class Bar and Both belong to class Foo.



Now Being in class Foo i want to just print the instance variable of class Bar.

Let me know if i am correct.

 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You had mention:


No, you have an Instance variable 'b' which belongs to class Foo and refers to an object of class Bar.



If you access a reference variable declared in your class as instance variable then only you need two reference variables.



I would like to say 'b' is not an instance variable .

In other words it is a reference variable which refers to an object of class Bar.

i was confused with your this statement.


If you access a reference variable declared in your class as instance variable then only you need two reference variables.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class has two types of variables: method and Instance.

method variables are local that is declared inside a method and instance variables are non local that is declred outside methods or say not declared in any of the methods in class.


here 'b' is not declared inside the method so it is an instance variable of class Foo. but 'f' is declared inside the main method so it is local or method variable in class Foo.

Hope you understand now.
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks Neha.

I thought variables with primitive data type declared inside a class but outside the method are called Instance variables.

eg: int a= 5 , double s = 6.7 and so on.

I never came through like:

Bar b = new Bar();

I thought b is an reference variable of class Bar.

But it is an instance variable of class Foo.

Thanks .
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Neha,

So


I know it seems to be silly but still i wnat to clear.

How B is related to A.

One object is created in Class B which is of type Class A.

Correct.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its Has-A relationship that is 'B' Has-A 'A'.
 
dimple bav
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its so simple.

I dont know why i am not able to indentify such simple things.

I have done with K&B 1,2,3(except Wrapping and Arrays) ,4 throughly.

Anyway i have to be alert.

 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Dimple thats fine, Even I had such problems initially like I couldn't recall those little things.
just read those chapters again and when you will do some mocks you will overcome these difficulties.
All the Best
 
reply
    Bookmark Topic Watch Topic
  • New Topic