3

I am facing a big problem in calling non static method from static method.

This is my code

Class SMS
{
    public static void First_function()
    {
        SMS sms = new SMS();
        sms.Second_function();
    }

    public void Second_function()
    {
        Toast.makeText(getApplicationContext(),"Hello",1).show(); // This i anable to display and cause crash
        CallingCustomBaseAdapters();    //this was the adapter class and i anable to call this also
    }

I am able to call Second_function but unable to get Toast and CallCustomBaseAdapter() method, crash occurs.

What should I do to fix that issue ?

5
  • There is no way to achieve your goal and it would be weird if there was an overcome. static methods and fields belong to classes and non-static methods and fields belong to class instances. To call a non-static method you need an instance of a class, and that's all. Hope this helps. Commented Oct 9, 2012 at 9:56
  • The member functions are being called on a specific instance. Commented Oct 9, 2012 at 9:59
  • @ Egor , yeah i agree with you, here i used Class instance to call non static method, and from Second_function(non static method) i want to call another method and custom adapters, but i cant.. how to do this? Commented Oct 9, 2012 at 9:59
  • Try to pass the context as a argument in the non-static method using static method. I means pass the context to static method and then pass same context to non-static method. Commented Oct 9, 2012 at 10:02
  • I think besides acquainting yourself with some Java code standards (e.g. capitalization) you should ask what are you trying to do. I'm not even sure how your code possibly compiles - SMS does not seem to have access to any context. SMS should extend some class that derives from Context. Your code would work fine if there was nothing inside the second method but there is stuff that isn't legal. Also, no one uses 1 as a Toast length, use Toast.LENGTH_{SHORT|LONG} Commented Oct 9, 2012 at 10:02

2 Answers 2

8
  public static void First_function(Context context)
  {
    SMS sms = new SMS();
    sms.Second_function(context);
  }

  public void Second_function(Context context)
  {
    Toast.makeText(context,"Hello",1).show(); // This i anable to display and cause crash
  }

The only solution to achieve this is that you need to pass the current context as a parameter. I wrote the code for only Toast but you need to modify it as per your requirements.

pass the Context from the your activity First_function(getApplicationContext()) etc..

for static string

public static String staticString = "xyz";

public static String getStaticString()
{
  return staticString;
}


String xyz = getStaticString();
Sign up to request clarification or add additional context in comments.

1 Comment

is it possible to move string value from static string to non static String?
1

You should have a reference to a Context. You are trying to get the application context from inside a SMS instance.

I guess you are calling the First_function from an Activity or Service. So you can do this:

Class SMS
{
    public static void First_function(Context context)
    {
        SMS sms = new SMS();
        sms.Second_function(context);
    }

    public void Second_function(Context context)
    {
        Toast.makeText(context,"Hello",1).show(); // This i anable to display and cause crash
        CallingCustomBaseAdapters();    //this was the adapter class and i anable to call this also
    }

Then, from your activity:

SMS.First_function(this); //or this.getApplicationContext() 

4 Comments

Can u tell me how to have a reference?
How is your code ANY better then the code that was originally written? SMS still has no reference to a context - it's just a class that doesn't extend activity. The original code was fine in the sense that non-static methods were not being called in a static context. Unfortunately, it seems Vishnu has no idea what's going on but that doesn't mean you just go along with it.
You'll have to post all your code if you want any help from me. I'm sorry but unfortunately I cannot understand what you're trying to do :(
lol ...stackoverflow users are funny sometimes.. the accepted answer is exactly the same as mine, but mine has one downvote.. excelent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.