1

What's the difference between calling a method through an object vs a class.

for example the Class Bob

public class SecretNumber() {
    public static int secretNumber = 2;
    public static void changeSecretNumber(){
        secretNumber++;
    }
}

What would be the difference if i called it like an object

SecretNumber secretNumber = new SecretNumber();
secretNumber.changeSecretNumber();

vs calling it like this

SecretNumber.changeSecretNumber();

How would one method effect the other?

Yicanis
  • 328
  • 1
  • 17
  • 2
    Now it'll compile. No difference, but calling a static method in an instance is misleading and confusing. – Dave Newton Jul 06 '12 at 01:14
  • okay, but if i had the object created and i called that method statically. how would it affect the int? Thanks in advance – Yicanis Jul 06 '12 at 01:16
  • possible duplicate of [Static method in Java can be accessed using object instance](http://stackoverflow.com/questions/4978000/static-method-in-java-can-be-accessed-using-object-instance) – Thilo Jul 06 '12 at 01:17
  • it was made feb. 2011 but if you mean i should have searched, i did and i guess it was my fault as i did not search with the correct keywords. Only after posting the example did i understand the concept. Either ways, thanks i get another point of reference. – Yicanis Jul 06 '12 at 01:19

4 Answers4

4

The result is the same.

You should call it by class name, not through the instance, because no dynamic dispatch happens.

Most compilers will give you a warning, if you do that, too. Some people argue that it should have been made a compile error.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
  • When you call a method on an object, the runtime will look at the actual runtime class of that object and call an overridden method if present. *This will not happen with static methods* (even though it might look like it if you call it through the instance). Static methods are always called in the class that you specified at compile-time. I.e. if your `secretNumber` was an instance of `SecretNumberSubClass`, the method in the parent class would still be called. – Thilo Jul 06 '12 at 01:18
  • ahh, okay. So by calling methods statically you will not be calling similar overridden methods from extends? Also if i made an instance of that object and then called that method statically, how would the method effect the variables in the class? – Yicanis Jul 06 '12 at 01:22
  • It does not matter if you have an instance of the class when you call a static method. It is exactly the same as `ClassName.methodName()`. The instance is not used at all. It can even be null! `SecretNumber s = null; s.changeSecretNumber()` will work. – Thilo Jul 06 '12 at 01:23
  • so if i called the static method and in the method it changed a private int. the int would not change at all correct? however if i called the same static method to change a static int the int would change? – Yicanis Jul 06 '12 at 01:30
  • You cannot access a "private" (if by that you mean instance variable) int in a static method. That won't even compile. – Thilo Jul 06 '12 at 01:32
  • +1; the typing bit is important. I was going to mention the null thing but that seemed like too much ;) – Dave Newton Jul 06 '12 at 02:17
0

In Java, unless a class method is "static", you can't call it except through an object instance.

Here's an example of a method where it would make sense to declare it "static":

http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

The second form isn't valid Java. You can only call methods on a class if they are declared static.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
0

Calling a method through a class is a static method. Unless you declare the method a static method, the compiler will give you a compile error .

rush00121
  • 185
  • 1
  • 5
  • 13
  • What would the effect be on the number though? for example if i called the static method would all objects carrying that number be changed or what? – Yicanis Jul 06 '12 at 01:13
  • The int secretnumber is a **static** number . That means that there is class variable and this will be the same for all the objects . So if you increment the number , then yes , the number will change for all the objects. – rush00121 Jul 06 '12 at 01:17
  • So if i changed it to public secretNumber instead of static it wouldn't be affected? – Yicanis Jul 06 '12 at 01:24