This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes not wanted methods getting invoked when created an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "not wanted methods getting invoked when created an object" Watch "not wanted methods getting invoked when created an object" New topic
Author

not wanted methods getting invoked when created an object

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hello world,
I am writting a program which has a structure like this.
public class x
{
public x()
{
}
}
class a
{
public a()
{
}
public void methodA()
{
}
}
class b
{
public b()
{
}
public void methodB()
{
}
}
The problem I am having is, if I create an object of, for example 'class b' and access the
methodB() from 'class a', not only the methodB() gets invoked but also other methods which
are there in the 'class b' gets invoked. Could you please explain what is wrong and how to
over come this. All the methods are public in the program.
Thanks


SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hi,
Are you making an object of class b from within class x? Is a method from class b being called from within the constructor or another method of class a? Class b has another method besides methodB which is being called?
For example, is your code something like this?:
public class x
{
public x()
{
a object_of_a = new a();
}
}
class a
{
public a()
{
b object_of_b = new b();
b.methodB();
}
public void methodA()
{
}
}
class b
{
public b()
{
}
public void methodB()
{
}
}
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Ransika and Kim -
When posting code, would you please use the UBB CODE tags and also indent code lines? The UBB codes format the code and make it much easier to read, as does indentation.
Cheers!
Jeff


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Just trying out the ubb code to see how it works, haven't used it before.
Thanks for the suggestion.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
BTW, you can also edit your old posts (and delete the once that become superfluous by doing so - just be aware of the fact that deleting the first in a thread deletes the whole thread...).


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
And just to point out the obvious, I think Kim meant to post a code example as follows.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: not wanted methods getting invoked when created an object
 
Similar Threads
Overriding private functions
Regarding Method Shadowing
Sub extends Base.....right?
Assigning sub class reference to super class object
Question on Powermockito Partial Mocking