• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling methods from another class  RSS feed

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to call a method from different classes using:


and getting the error "non-static getStrength() cannot be referenced from a static context"

The method in both classes is identical:



I know I'm missing something very simple, but not seeing it.
 
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
You are calling the methods as if they were static methods, when in fact they are instance methods. To call an instance method you need an instance of the class.
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Please UseCodeTags when you post a snippet of code - it would make your code more readable (in an editor you can select your all code lines and click on "Code" button in an upper editor panel).

Likely you're trying to call the code directly via class name. And since method is not a static - it is illegal. You have to instantiate an object of that class and only then you can invoke the method on particular object of the class.
Could you please post a bit more code around, so we could see which particular part you have to amend.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I'm sure there are more errors and still have more work to do on this before attempting to run it
 
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand what attack() is supposed to do, you want to go through the colony ant by ant and pit one any against another. To do this you need a way to get the ant list from a colony. Add something like getAnts() to IColonyBase then implement it in BlackAntColony.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The attack method is supposed to cycle through the ants in each colony, compare the strength of the two ants and will add the index of the ant to an ArrayList so that ant can be removed. I stopped writing the attack method once I found the errors
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there is a getAnt method in the IColony and both red and black colonies

I tired



and get the same message
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's look at it this way. You're Java. Someone has just written...

You know you are calling the getStrength() method from the RedAnt class, but which ant? This is the problem with calling an instance (individual) method in a static (class) way. You need a specific ant, an instance of the RedAnt class.

So how do you get a specific ant from the Red Ant Colony? You need another method that returns the list of ants in the colony. Then you can iterate over the list and get individual ants.
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what about this?

Here you have a similar problem. Which Red Ant Colony? You need an instance of the class. The object reference competitor should work.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried that too, same error message. Sorry I didn't reply sooner. Have been on the road all day.
 
Ranch Hand
Posts: 51
IntelliJ IDE Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,



Should be something like:


to make it technically work. However, your code doesn't really look very object oriented to me.
Try to not give out internal details like strength. Let the ant itself compare to another ant,
and let it just return something like "0" - we are of equal Strength, "1" - I am stronger and "-1" I am weaker - which you can easily do by implementing the
java.lang.Comparable interface (more details below).
Here are some further thoughts of what to improve:

----

what this method does is it just compares the strengh of two Ants.
So why not call this method compareStrength(IAntBase competitor)?
The fight method I see more in a class called something like "Arena", that gets two ants as arguments or all ants in a list
and lets them fight and returns the winner as the result.

Why do you start interfaces with "I"? That something is an interface should not be visible.
-----------
Boolean compares:

just say:

(However you have not included the case that both ants could be of equal strength.
Think about simply implementing the java.lang.Comparable interface, this way you could very simply
compare an entire list of ants and get the first, second, third... winner.


----


just say:
 
Knute Snortum
Sheriff
Posts: 4293
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried that too, same error message.


Post what you tried. competitor is named as if it's a single ant, but it's a colony, right? So first you need to get an ant, then get its strength. Then you need to do the same thing for the current colony. Post your code, even if it doesn't work.
 
Greenhorn
Posts: 4
Opera Pulse IDE Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can easily call the method in another class, by making object of that class in the other class and call with this object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!