-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();
        }

    }
}
7
  • 1
    func isn't a static method. You're trying to call a non-static method without an instance to call it on. Commented Jul 14, 2016 at 4: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) Commented Jul 14, 2016 at 4:41
  • Possible duplicate of : stackoverflow.com/questions/290884/… Commented Jul 14, 2016 at 4:44
  • 2
    You don't need any object instance to call a static method. You call a static method like ClassName.method() Commented Jul 14, 2016 at 4: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. Commented Jul 14, 2016 at 4:54

3 Answers 3

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.

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

Comments

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

4 Comments

The reason he gets a NullPointerException is p.x = 10; - it never gets to p.func(); in the try block because of that.
@ErwinBolwidt - I am well aware, and that's besides the point. I said would be executed if not caught
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.
@ErwinBolwidt - I've rephrased my answer
-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.

2 Comments

I don't know you people are giving answer because this question has multiple answers on SO and on google too.
@MehradMalik - the question is not a duplicate to what I assume you are referring to. The code in the question actually compiles

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.