-5

Static methods are as close as possible to a global method. so why is this type of method call not possible? is there any other way to call the static method without instantiating the class??

import java.io.*;
import java.util.*;
class pgm
{
    int x,v;
    static void func()
    {
        System.out.println("Function run");
    }
}
class egs
{
    public static void main(String args[])
    {
        pgm p=null;
        pgm.func();
        try
        {
            p.x=10;
            p.func();
        }
        catch(NullPointerException e)
        {
            e.printStackTrace();
        }

    }
}
Nn Karthik
  • 122
  • 3
  • 13
  • 1
    `func` isn't a static method. You're trying to call a non-static method without an instance to call it on. – user2357112 Jul 14 '16 at 04:40
  • 1
    `egs.func()` doesn't exist... I'm not sure why you expect that to work. Static methods are not global, they are still bound to the class. In any case, you don't even have a static method (other than main) – OneCricketeer Jul 14 '16 at 04:41
  • Possible duplicate of : http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static – Mehraj Malik Jul 14 '16 at 04:44
  • @cricket_007 is there a way to call static methods from another class in same program. without using an object for the class – Nn Karthik Jul 14 '16 at 04:48
  • I don't understand what the problem is. Your code compiles fine. – Sotirios Delimanolis Jul 14 '16 at 04:49
  • 2
    You don't need any object instance to call a static method. You call a static method like `ClassName.method()` – OneCricketeer Jul 14 '16 at 04:50
  • 1
    You've made some corrections to the code, and now the problem is in the `p.x = 10;`. p was assigned null and not subsequently reassigned. Therefore, you get a NullPointerException. – Ken Geis Jul 14 '16 at 04:54

3 Answers3

1

I think you are confused.

pgm.func();

Is the correct way to call the static method. Whereas...

p.func();

even if p is null (as it is in your code) would still be executed because the compiler really uses the first way since it knows that the method is static.

Sidenote: you are catching the NullPointerException at p.x, so func() is not excuted in this example.

So to answer,

Is it possible to call a static method of another class from a non static method without instance?

Yes, because you never needed the instance to call the static method

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • The reason he gets a NullPointerException is `p.x = 10;` - it never gets to `p.func();` in the try block because of that. – Erwin Bolwidt Jul 14 '16 at 05:06
  • @ErwinBolwidt - I am well aware, and that's besides the point. I said would be executed if not caught – OneCricketeer Jul 14 '16 at 05:07
  • Of course it is part of the point. When the OP runs his code he gets a NullPointerException. `p.func();` would not be executed unlike you claim, because the `p.x = 10;` caused a NullPointerException. I agree that the OP shouldn't put that statement in his code because it's unrelated to his problem, but it's very likely one of the sources of the OP's confusion. – Erwin Bolwidt Jul 14 '16 at 05:09
  • @ErwinBolwidt - I've rephrased my answer – OneCricketeer Jul 14 '16 at 05:13
1

firstly modifier static not allowed at class pgm.

If you want to call func and x in class legs.

You must use public final then your class name and declare all member of class as static.

After then you need to get reference for class pgm.

So your code will be

  import java.io.*;
  import java.util.*;
  public final class pgm
  {
     static int x,v;
     static void func()
     {
      System.out.println("Function run");
     }
   }

  class egs
  {
   public static void main(String args[])
   {
     pgm p=null;         //ref here 
      p.func();          // use ppm func here 
      try
      {
        p.x=10;
        p.func();
      }
      catch(NullPointerException e)
     {
        System.out.println("Null caught");
     }

  }
}

You would get what you want.

Never get confused static used for compile whole block, method, variable at compile time so you can call anything which is static at run time without any instantiation (using new).

Right way I provide you above.

Dheeraj Upadhyay
  • 336
  • 2
  • 12
-1

You can't call a static method or variable from a non-static method because the static variables are like sharing a similar set of memory acquired by them and then can be used as preferred. So it got separated from the remaining memory resources. Java freed its acquired resources as soon as its done with them but static member lasts till the end of execution.

Ashutosh Sagar
  • 981
  • 8
  • 20
  • I don't know you people are giving answer because this question has multiple answers on SO and on google too. – Mehraj Malik Jul 14 '16 at 04:50
  • @MehradMalik - the question is not a duplicate to what I assume you are referring to. The code in the question actually compiles – OneCricketeer Jul 14 '16 at 04:57