8

I am new to JAVA, and I like to try and understand everything.

When accessing a static method "hero.returnHp()" in JAVA, I have the following:

 hero Mike = new hero();

 Mike.returnHp();

The program runs fine, but I notice that Eclipse has a warning stating, "The static method from the type hero should be accessed in a static way." When I accept the auto-fix, it changes "Mike.returnHp();" to "hero.returnHp();".

So I have two questions:

1) What is the advantage of this?

2) If I created two objects of the same type, how would I specify which one to return when accessing in a static way?

Thanks!

7
  • 2
    The point of static methods is that they are not tied to instances of the class. So if you call one with an instance on the left of the dot, or the class name, doesn't make a difference. Commented Nov 24, 2012 at 1:24
  • 4
    Well, one problem is that you're using leading caps for variable names and leading lower case for classes -- exactly opposite the accepted standard. But, IIRC, either form you use above is technically legal, though the "preferred" form is to use the class name when calling a static. Commented Nov 24, 2012 at 1:31
  • "When I accept the auto-fix, it changes Mike.returnHp(); to hero.returnHp(); Mmmm rather the other way, I guess. Commented Nov 24, 2012 at 1:32
  • @leonbloy No, since it's static, the fix is to call it with the class name. The class name is lower case here, that might have misled you. Commented Nov 24, 2012 at 1:33
  • 1
    Offtopic: As @HotLicks mentions, your way of using upper and lowercase is the oposite of what the Java community uses and it will tend to confuse people. I strongly advise you to learn the common way now that you are starting. bitprison.net/java_naming_conventions Commented Nov 24, 2012 at 1:44

5 Answers 5

11

I would first like to point out what the keyword static means.

Static variables only exist once per class – that is, if you create a class with a static variable then all instances of that class will share that one variable. Furthermore, if it’s a public static variable, then anyone can access the variable without having to first create an instance of that class – they just call Hero.staticVariableName;

Static method/functions are stateless. That is, they act only on information (1) provided by arguments passed to the method/function, or (2) in static variables (named above), or (3) that is hard-coded into the method/function (e.g. you create a static function to return “hello” – then “hello” is hard-coded into the function).

The reason why Eclipse wants you to access static methods in a static way is because it lets you and subsequent programmers see that the method you’re accessing is static (this helps to prevent mistakes). The function will run either way you do it, but the correct way to do it is to access static functions in a static way. Remember that if you call a static method, no matter what instance variable you call it from (Tim.returnHp, Jim.returnHp, Mike.returnHp, whatever) you will call the same function from the hero class and you will see the exact same behavior no matter who you call it from.

If you created two objects of the same type then you COULD NOT specify which one to return when accessing in a static way; static functions/methods will refer to the entire Hero class.

Can you explain what you’re trying to do so that we can offer more specific feedback? It’s quite possible that returnHp() shouldn’t be static.

Is that “return hit points”? If it is, then you do NOT want it static because the number of hit points that a hero has is part of the hero’s state, and static methods are stateless. (Think of state like the current condition – alive, dead, wounded, attacking, defending, some combination of the aforementioned, etc.) I would recommend going into the Hero class and changing returnHp to a non-static method.

Now… I know you didn’t ask, but I would like to advise you of something:

Class names (such as Hero) should be capitalized. Instance variable names (such as mike) should be lowercase. This is a widely accepted naming convention and it will increase the readability of your code.

Jeff

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, this is exactly what I was looking for Jeff! You are correct, I am using this to set hit points for a character in a basic battle simulator. I was working on creating methods for different actions (attack, run, item) but I noticed I couldn't access them in another class without making them static and extending the class. I was trying to create these methods so I could call them when I needed them instead of writing another block of arguments for each battle. This was the best way I could figure out.
You're welcome, Hermes. Can you clarify -- You said "I couldn't access them in another class without making them static and extending the class." When you say another class, do you mean a subclass of Hero? What other class were you trying to access them from? Also, were you labeling these as private attack() private run() etc, or were you labeling them as public attack() public run(), etc.
Hi Jeff, sorry for the lack of information; I am still getting used to making my information as clear as possible. I have three classes: Hero, Monster, and Battle (Main). I was working on making a new class, "Fight", to hold methods relating to Attack. The object Hero is created in the Battle class. I was trying to access the variables related to the Hero class in the Battle class. The variables in the Hero class and are written as private and use setter and getter methods to be accessed.
Don't apologize; you're doing fine. So Battle is the static main method. If you're creating instances of Hero inside of Battle, then the only reason why I imagine that you would be unable to access mike.attack() is that you declared attack() as private, whereas attack() should have been declared public (and not static). (Keep your variables private and use setters/getters, though -- that's good.) Might I ask what the "Fight" class is to be used for? How does it fit into your designs?
Ahh, that makes sense! I was trying to spread the code out into methods so it wouldn't all be in the main method. The "Fight" class was going to hold the "Attack" method which I could call each time the player fought. The game is very simple now and only has two battles: You face one monster and after you defeat it a boss comes. I was trying to write the Attack into a method so I could just call it once the Boss came instead of rewriting the code blocks again.
|
3

A static method is one which belongs to a class but not to an object. In your example above, you have created an object Mike of class hero. The method returnHp() is static, and belongs to the hero class, not the hero objects (such as Mike).

You will likely get an IDE or compiler warning when you reference a static method from an object, because it should never be tied to that object, only to its class.

Based on the method name, I would guess it shouldn't be static.

class hero {
    private float hp;
    public float returnHp() { // Should NOT be "public static float ..."
        return hp;
    }
}

The JavaDocs on class members has a brief discussion on statics as well. You may want to check that out.

1 Comment

@DanielFischer Sorry, that should read "warning". My bad habit to write "error" when things go wrong.
1

A static method is completely independent of any instances of the class.

Consider that this works, and does not result in a NullPointerException:

 hero Mike = null;

 Mike.returnHp();

(by the way, class names should start with a capital, and variable names be lowercased).

Here is another neat example: Being a static method, Thread.sleep always sleeps the current thread, even if you try to call it on another thread instance.

The static method should be called by class name, not through an instance, because otherwise it is very confusing, mostly because there is no dynamic dispatch as static methods cannot be overridden in subclasses:

 hero Tim = new superhero(); // superhero extends hero

 Tim.returnHp();  // still calls the method in hero, not in superhero

You are getting a compiler warning now, but many people say that this was a design mistake and should be an error.

Comments

0
  1. It is part of the JVM spec.
  2. You don't need to. A static method is common between instances of a class, your confusion arises from thinking it is an instance method.

Comments

0

static means a static way. One reason to use static is you can access it using class directly. that is its benefit. that is why main is always static. The entrance function don't need to create an instance first. Actually if you search static in google, and understand it deeply. U will know when and why use static.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.