2
Public class Main {

    public static String s;
    String a="hello";
    String b="World";
    s=a+b; 

    class Demo{
        String m;
        m=Main.this.s; 
        System.out.println(m);
    }

}

Output should be : Hello World

I just here try to understand main thing. My Main class is a Android Activity class which extends ListActivity and Demo class extends AsyncTask. I need to read value Static String s from doInBackground method.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41

3 Answers3

0

When you want to access any type of variable's value from InnerClass, you can dirrectly call that variable.

For this case, You have no need to use Main.this to access the our class's static variable...just call it as below...

m = s;

When you want to access a static object of one class from another class which isn't inner class then you need to use Class Name as...

m = Main.s;

not as...

m = Main.this.s;

So, your class should look like as below...

Public class Main{

    public static String s;
    String a="hello";
    String b="World";

    s = a + b; 

    class Demo{

        String m;
        m = s; 
        System.out.println(m);

    }
}

Update:

The result giving you null because you are inserting non-static value to a static object. Here, a and b are non-static and s is static. So, to get correct value you have to declare all of them as static as below...

    public static String s;
    static String a="hello";
    static String b="World";
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
0

An Inner class can always directly access all the variables without using any object or class name. Hence you can directly refer to your outer class's variable without metioning its name.

Instead of m=Main.this.s; You can directly access variable s as m=s;

Its basic rule of Object Oriented Programming that Inner class has always full access of all the methods and variables of outter class.

user2060383
  • 979
  • 1
  • 14
  • 32
0

- First i won't show you the exact example what u mentioned here, because your problem is not this example but to understand how to access inner class members.

Modified Example:

package com.cyberfreaky.test;

public class Main {

    public static String static_string = "hello";
    public String nonstatic_string = "world";

    class Demo {

        String m = "";
        String n = "";

        public void getStaticString() {

            m = static_string;
            System.out.println(m);
        }

        public void getNonStaticString() {

            n = nonstatic_string;
            System.out.println(n);

        }

    }

    public static void main(String[] args) {

        Main outerClass = new Main();
        Main.Demo innerClass = outerClass.new Demo();

        innerClass.getStaticString();
        innerClass.getNonStaticString();

    }

}

In case of Android you can access the Outer Class member from Inner Class as below:

package com.example.simpleservice;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {

    int i = 0;
    static int x = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    class InnerClass extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            int a = i;
            int b = x;

            return null;
        }

    }

}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
  • In the second example, int x is static and i not. But int a = i; int b = x; Assignment process is same ? –  Mar 06 '14 at 07:48