File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Scope and classes/methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Scope and classes/methods" Watch "Scope and classes/methods" New topic
Author

Scope and classes/methods

Chadd Franck
Ranch Hand

Joined: Nov 05, 2008
Posts: 50
I have a program I am writing, mostly to show myself what fundamentals I know and which I don't understand. I have read Head First Java, Java 24hrs, and Java for dummies. I don't know really how to ask my question, but I'll try;

I have a class A, and a class B.



What am I really passing here to ClassA or go() in classa:

[from above code]
ClassA classa = new ClassA(classb);
classa.go(classb);
[/from above code]


I'm trying to pass the remote of classB to classa so everything in classa has scope of classb via classb.doStuff().

Thanks again
[ November 28, 2008: Message edited by: Chadd Franck ]
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Please use code tags and dont mix normal text in the middle of the code. I find it impossible to know what you are asking in the above post, and the code make no sense.


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
It helps if you post code that actually compiles, but in answer to your question classb is a reference to a classB object and when you call a constructor or method you pass a copy of that reference.


Joanne
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Tom, please check your PM for an administrative message


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Chadd Franck
Ranch Hand

Joined: Nov 05, 2008
Posts: 50
Originally posted by Joanne Neal:
It helps if you post code that actually compiles, but in answer to your question classb is a reference to a classB object and when you call a constructor or method you pass a copy of that reference.


I can't get the code to work, so I can't post code that compiles sorry,
here is the next part of the question;

if newRemote = classb, in the constructor of ClassA; can I access the ClassB object via newRemote ie; newRemote.doStuff(); from any public method?

Or did I mess up here with the constructer for classA?
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
if newRemote = classb, in the constructor of ClassA; can I access the ClassB object via newRemote ie; newRemote.doStuff(); from any public method?


Yes absolutely, but not the private members/methods.



This will compile fine as long as you have defined a reference variable of type ClassB called newRemote. From your first post, the following code does not have that, so the compiler doesnt know about newRemote:


Also, if you are passing an instance of classB to the constructor of classA and saving that refernece as newRemote, there should be no need to pass the classB instance again to the go() method.
Chadd Franck
Ranch Hand

Joined: Nov 05, 2008
Posts: 50
Great, thanks for the help, the next question,

Where would be the best or only place to decalare newRemote?

Should I declare it like ClassB newRemote = new ClassB();
or ClassB newRemote;?


As refering to:

--------------------------------------------------------------------------------
if newRemote = classb, in the constructor of ClassA; can I access the ClassB object via newRemote ie; newRemote.doStuff(); from any public method?
--------------------------------------------------------------------------------

You said:

Yes absolutely, but not the private members/methods.


I know I read somewhere that objects have access to private methods/members of another obj of the same type.

What am I missing here?
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142

Should I declare it like ClassB newRemote = new ClassB();
or ClassB newRemote;?


They are similar but different!

1) ClassB newRemote; // This just declares the reference, its value is null
2) ClassB newRemote = new ClassB(); // This declares it & gives it a value

Since you have defined a constructor that takes a ClassB parameter, it would be usual to use method 1) and assign the passed in value as part of the constructor:


Above we assign the classB reference to newRemote. We can then use the newRemote reference to call methods from ClassB, as shown. In this design, ClassA is not responsible for creating the ClassB instance, it receives it in its constructor.

You would use method 2) ClassB newRemote = new ClassB(); when you want to be in control of the creation of ClassB and what values you want to pass to the constructor(if it took parameters). Using this design, you would not then need to pass ClassB into to the construtor, as you already have created the instance of ClassB yourself:


Method 1) is more common. It's also easier to test ClassA in isolation using method 1) as you can control what is passed to it externally in the test class, rather than ClassA controlling it itself.

I know I read somewhere that objects have access to private methods/members of another obj of the same type.

Yes, thats true. The most common place for this to be used in when overriding the equals() method from the class Object. For example:
Within ClassA, if the main identifier for equality in ClassA was an int called "id" then you can do the following


[ November 28, 2008: Message edited by: Tom Johnson ]

[ November 28, 2008: Message edited by: Tom Johnson ]
[ November 28, 2008: Message edited by: Tom Johnson ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

Originally posted by Tom Johnson:

The above implementation of equals can break symmetry with subclasses.

Let's say we have ClassC that extends ClassA and uses "instanceof ClassC" inside its equals method. In that case, the following is a problem:

That's why I always use exact class matching:

This way, a.equals(c) will also be false.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Cheers Rob! Good point, I hope I haven't made that mistake in production I have a feeling it would take quite a while to track down!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

It's also something I had to pick up from an article on JavaWorld.

instanceof is still perfectly legal and valid though if the equals method cannot be overridden - either by making the method final or the entire class. That way, subclasses will also check for "instanceof ClassA" and symmetry is maintained.
Chadd Franck
Ranch Hand

Joined: Nov 05, 2008
Posts: 50
Wow, great. Thanks for all the help I was able to create hours of code now that I understand that. You guys are great.
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
No bother
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scope and classes/methods