• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using method of another class without passing that class  RSS feed

 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I would like to use a method from another class, but I cant change the method to pass it the class (its an implemented method) nor I can create an instance of the class in this class (willl mess up my program already created an instance somewhere else). Is there a way to do this?

 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things: should the method setText() be static? Then you would just need the class name. Second, why would creating an instance of the class "mess up your program"? Or can you use the instance already created?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If setText() is not static, do not make it so just "to work around this problem".

To call an instance method, you need a reference to the object instance. And yes, just creating a new instance so you can call the method doesn't make any sense. You need a reference to the instance of the object that you wish to affect.
 
Campbell Ritchie
Marshal
Posts: 56597
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Bear that making things static just so they compile is almost always a serious mistake.
Please explain what that actionPerformed method is supposed to do. It looks very confused and confusing to me.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea I thought about changing it to static but its used in a lot of classes so its not a good idea like Bear said.
About the ActionPerformed it checks the button clicked and adds the value that is inputted by the user to the label of the empty button.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to use an instance already created in another class? Like lets say I created an instance in main can I use that instance?
I don't really think so but it wont hurt to ask
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you pass the reference in, yes.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea but sadly I cant pass the class because its an implemented method changing it will give me an error
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say I change it to static or create another method that is static Will it work normally(change the labels of the buttons without a problem) I am asking because I haven't practiced much with static methods I usually only create non static methods to use with objects
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I (now) don't think it will work to have a static method because setText() acts on an object, right? And a static method has no object instance.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I (now) don't think it will work to have a static method because setText() acts on an object, right? And a static method has no object instance.

Yea thats what I thought but when you told me about changing it to static I thought maybe it could work.
Would it help if I add more code here?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you can access a field from the actionPerformed() method if you pass in the object that needs the setText() to the class that encloses the anonymous class. So like this:



This code is just giving you the idea. ActionPerformer is just a name I gave to the interface that has actionPerformed() in it.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:I believe you can access a field from the actionPerformed() method if you pass in the object that needs the setText() to the class that encloses the anonymous class. So like this:



This code is just giving you the idea. ActionPerformer is just a name I gave to the interface that has actionPerformed() in it.

Thanks, that is a great idea and would work great if I am using 1 or 2 buttons , but sadly I am using 81 buttons in an array of 9 x 9 so setting them all in the constructor would be crazy
 
Campbell Ritchie
Marshal
Posts: 56597
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote: . . . . . .
Wouldn't that be?
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found a solution, thanks guys
 
Campbell Ritchie
Marshal
Posts: 56597
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what was the solution?
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Wouldn't that be?

The OP was saying that the method was "implemented". I interpreted that as meaning it was in an anonymous class. But yes, you could be right.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And what was the solution?

I had to use all the code for the TextField in this class and create a method in the other class to add it to the panel
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!