aspose file tools*
The moose likes Beginning Java and the fly likes Instance method access from other objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Instance method access from other objects" Watch "Instance method access from other objects" New topic
Author

Instance method access from other objects

Rob Sawkins
Greenhorn

Joined: Dec 20, 2007
Posts: 1
In the following BROKEN CODE, I get "container cannot be resolved".
Obviously, I can change the code as shown below in WORKING CODE
to pass a reference to the container instance. However, what I want
to know is why I can't access the instance variable referencing the
container object directly from within the user instance?

None of my Java books really talk about this. The way I started to
think about this is that the container is a local variable of the
main method holding an object reference. To reference this variable
outside the main method scope, I need to use a qualified name. But
it doesn't seem that there is a way to do this. Then I started
thinking this cannot work because when the compiler compiles the
User Class, without any other context, it has no way of knowing what
"container" is because it was not defined locally, or passed in. Is
this the reason, or am I missing it? Maybe I'm confused between
compile time and run time?

By the way, is there a specific place in the java spec or some
website tutorial that touches on this, or is it simply "known" from
working knowledge of Java?

Thanks in advance.



BROKEN CODE:
--------------------------------------------------------------------
package testPkg;

public class AppEntry {

public static void main(String[] args) {
Container container = new Container();
System.out.println(container.getVal());
container.setVal(100);
System.out.println(container.getVal());
User user = new User();
user.go();
}
}
--------------------------------------------------------------------
package testPkg;

public class Container {

private int val = 0;

public int getVal() {
return val;
}

public void setVal(int inVal) {
val = inVal;
}
}
--------------------------------------------------------------------
package testPkg;

public class User {

public void go() {
System.out.println(container.getVal());
container.setVal(99);
System.out.println(container.getVal());
}
}
--------------------------------------------------------------------



WORKING CODE:
--------------------------------------------------------------------
package testPkg;

public class AppEntry {
...
...
user.go(container);
}
}
--------------------------------------------------------------------
package testPkg;

public class User {

public void go(Container inObj) {
System.out.println(inObj.getVal());
inObj.setVal(99);
System.out.println(inObj.getVal());
}
}
--------------------------------------------------------------------
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Welcome to the Ranch.

Please use "code" tags around code in future; it preserves the indentation and makes it a lot easier to read.

Your working code looks fine to me, but I can suggest some improvements.

You have a reference to an object in one class, ie there is a reference to "container" in the AppEntry class. [Not an AppEntry object because you are in a static context.] This is however retained inside the AppEntry class. Since it is declared inside a method, its scope is only that particular method, and it "ceases to exist" when you reach the end of that method.

Your "container" is a reference to a Container, and when you write "= new Container();" the JVM creates a Container object and moves the "container" reference to point to that object. So now we haveBut you haven't provided an way for "user" to get at it.

In the code which works, however, you have passed the reference to "container" as a parameter, so the go() method has access to the "container" reference. So it all works.

I would suggest these improvements:
  • Change package names to be all lower-case in future.
  • You don't need to set the "value" field to 0 in the Container class. Fields are set to 0 (numbers) false (booleans) or null (objects) as a default.
  • I suggest you make the "container" a private field of the User class, and pass the "container" reference to the User constructor.
  • I hope I have been of some help.
    CR
    [ December 20, 2007: Message edited by: Campbell Ritchie ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Instance method access from other objects