3

Possible Duplicate:
calling non-static method in static method in Java

Is it possible to call a non-static method from a static method? The non-static is located in another class and it have to be non-static.

    public static void start() {
        CheckConnection checkInternet = new CheckConnection();
        if (checkInternet.isNetworkAvailable()) {
            // Has internet Connection
            } else {
            // No Internet Connection
        }
 }

The code doesn't give any errors in Eclipse (Android 4.0.4) but if I run it my application freezes and closes.

Community
  • 1
  • 1
ObAt
  • 2,247
  • 3
  • 23
  • 43
  • 5
    Yes it's possible (it would not compile if not). And it is not the reason why it freezes. More likely the `isNetworkAvailable()` method freezes for some other reasons. – assylias Oct 19 '12 at 11:44
  • or the c'tor of CheckConnection. Looking on the stacktrace when running in debug mode can unveil the problem. – IllegalArgumentException Oct 19 '12 at 11:47
  • In a java program (not specifically android) Main is a static method.. :) .. and it calls everything. so the answer to your title is yes. like all guys are suggesting here something else is going on. @AndroSelva it's not a duplicate, the question is not related to the title that's all – quinestor Oct 19 '12 at 11:47

5 Answers5

5
The only way to call a non-static method from a static method is you should have
an instance of the class containing the non-static method.

Like in your question :

 CheckConnection checkInternet = new CheckConnection();
    if (checkInternet.isNetworkAvailable()) {
        // Has internet Connection
        } else {
        // No Internet Connection

You have instance of CheckConnection so you can call it.

So there is no problem in your code with non-static method from a static method may be some other thing is responsible for application freezes.

Sumit Singh
  • 24,095
  • 8
  • 74
  • 100
1

Yes, this is possible. The problem must lie with your isNetworkAvailable method.

If it were impossible to call non-static methods from static methods, Java applications wouldn't work, since main is itself static.

Sumit Singh
  • 24,095
  • 8
  • 74
  • 100
Neil Weightman
  • 341
  • 2
  • 4
1

Just create the object of the class that contains non-static method you want to use and then you can call.

For example :

public class Test {
  public static void main(String[] args) {
        System.out.println("This is a static method");
        TestClass class1 = new TestClass();
        class1.nonStatic();
     }
 }

class TestClass{
    public void nonStatic(){
        System.out.println("This is a non-static method");
    }
}

In your problem

public class A{
   public static void start() {
        CheckConnection checkInternet = new CheckConnection();
        if (checkInternet.isNetworkAvailable()) {
            // Has internet Connection
            } else {
            // No Internet Connection
        }
   }
}



public class CheckConnection {
    public boolean isNetworkAvailable()
      {
        //some code
      }
   }

If this is the scenario then it should work.

Sumit Singh
  • 24,095
  • 8
  • 74
  • 100
Shreyos Adikari
  • 12,089
  • 19
  • 71
  • 81
1

Debug into your isNetworkAvailable() method. If possible have a try catch block inside that method and print the stacktrace of exception.

Sumit Singh
  • 24,095
  • 8
  • 74
  • 100
Sumit Desai
  • 1,455
  • 9
  • 19
1

Is it possible to call a non-static method from a static method?

Yes.

Since you have created an object of CheckConnection, you can call the method using it's reference. else it'll be a compile-time error.

but if I run it my application freezes and closes.

Calling non-static method from static context is not the reason. May be reason is within isNetworkAvailable.

Azodious
  • 13,563
  • 1
  • 33
  • 68