aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Trying to access the instance variable of Class Bar from Class Foo --------- why error ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Trying to access the instance variable of Class Bar from Class Foo --------- why error ?" Watch "Trying to access the instance variable of Class Bar from Class Foo --------- why error ?" New topic
Author

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

dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90



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.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
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.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
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:



SCJP 1.6 96%
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
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

Joined: Oct 30, 2009
Posts: 504
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

Joined: Sep 12, 2008
Posts: 90
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

Joined: Oct 30, 2009
Posts: 504
yup
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90

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

Joined: Oct 30, 2009
Posts: 504
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

Joined: Sep 12, 2008
Posts: 90
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

Joined: Sep 12, 2008
Posts: 90
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

Joined: Oct 30, 2009
Posts: 504
Its Has-A relationship that is 'B' Has-A 'A'.
dimple bav
Ranch Hand

Joined: Sep 12, 2008
Posts: 90
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

Joined: Oct 30, 2009
Posts: 504
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trying to access the instance variable of Class Bar from Class Foo --------- why error ?