• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scope and classes/methods

 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom, please check your PM for an administrative message
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No bother
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic