6

in java we are able to "invoke a static method with class name" and also "invoke a static method with an object" what is the difference between "invoking a static method with class name" and "invoking a static method with an object" in java?

madan
  • 149
  • 4
  • 12

6 Answers6

5

There is no difference but the recommendation is to call the static methods in a staic way i.e. using the ClassName. Generally static analayzers will report an error if you don't do so.

An important thing to understand here is that static method are stateless and hence calling them with an instance is confusing for someone reading your code. Because no matter what instance you use to call the static method, result will remain the same. This is because a static method belongs to a class but not to an object.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Yes, right on. The classic example of confusion is calling `anotherThread.sleep()` - of course, that's a static method and so the calling thread (rather than `anotherThread`) will be the one that sleeps. – vikingsteve Jul 10 '13 at 07:48
1

Internally, new ClassName().staticMethod(); is considered as ClassName.staticMethod(). So there is no difference. It just causes confusion. Consider an example

public class StaticDemo {

    static int staticVar = 10;

    public static void main(String [] args){

    System.out.println(new StaticDemo().staticVar);
    System.out.println(StaticDemo.staticVar);

    }

}

This outputs

10
10

`

even if you write new StaticDemo().staticVar, it will be considered as StaticDemo.staticVar. So there is no difference at all and always use the Class notation to avoid confusion.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
1

No, there is none. Apart from possible confusion... A reader of such code cannot reliably tell static from non static members/methods apart if you use an instance to access them:

public class X
{
    public static final String foo = "foo";
    public static String bar() { return "bar"; }

    public static void main(final String... args)
    {
        final X x = new X();
        System.out.println(X.foo); // "foo"
        System.out.println(x.foo); // "foo" too -- same reference
        System.out.println(X.bar()); // "bar"
        System.out.println(x.bar()); // "bar" too -- same reference
    }
}

As this is confusing, it is generally preferred to use the class to refer to such members/methods.

fge
  • 119,121
  • 33
  • 254
  • 329
1

There is no difference at all. But since static methods are at the class level, you can access them using the class name. Though invoking them using class object is possible, it creates a confusion. Why would you like to invoke them for every object if its value is going to be the same?

paary
  • 421
  • 6
  • 16
0

Static methods do NOT operate on a class. They are just bound to class instead of to member of that class. This means that they don't have access to any non static members of the class. Other than that, they are not very different than the non-static methods.

Michael Shrestha
  • 2,547
  • 19
  • 30
0

In SCJP guide book. having a example as following:

class Frog {
static int frogCount = 0; // Declare and initialize 
// static variable
public Frog() {
frogCount += 1; // Modify the value in the constructor
}
}
class TestFrog {
public static void main (String [] args) {
new Frog();
new Frog();
new Frog();
System.out.print("frogCount:"+Frog.frogCount); //Access 
// static variable 
}
}

But just to make it really confusing, the Java language also allows you to use an object reference variable to access a static member

Frog f = new Frog();
int frogs = f.frogCount; // Access static variable 
// FrogCount using f

In the preceding code, we instantiate a Frog, assign the new Frog object to the reference variable f, and then use the f reference to invoke a staticmethod! But even though we are using a specific Frog instance to access the staticmethod, the rules haven't changed. This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a staticmethod or variable, but the staticmember is still unaware of the particular instance used to invoke the staticmember. In the Frog example, the compiler knows that the reference variable f is of type Frog, and so the Frog class staticmethod is run with no awareness or concern for the Frog instance at the other end of the f reference. In other words, the compiler cares only that reference variable f is declared as type Frog.

I hope this will help you

ThiepLV
  • 1,219
  • 3
  • 10
  • 21