1

I'm studying inner classes in java. I have seen that if inner class is non static then it can easily access the outer class variable. But what if inner class is static, then how can we take a access of outer class's variable using static's class object ?

Below is my code, where am accessing outer class variable from inner class

package org;

public class Outerclass {

     String name = "Europe";

    public String getname() {

        return name;
    }

    public void setname(String name) {

        this.name = name;
        System.out.println(this.name);
    }

    static class innerclass {

        void updatename() {
            Outerclass o = new Outerclass();
            o.setname("USA");
        }

    }

    public static void main(String[] args) {
        Outerclass b = new Outerclass();
        b.name; // why this error here ? "Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration"

        innerclass i = new innerclass();
        i.updatename();

    }

}
1
  • 2
    Instead of showing us how you're trying to do something (and failing) why won't you explain what is it you're trying to achieve ? Commented Oct 28, 2017 at 16:25

2 Answers 2

2

You can't access non-static contents inside the static content

When we create static inner class by default it will created as a outer template as a association of inner template. So we can load both together but only static things can be inside the static inner class.

Now there are no connection between objects of the classes. But there are connection between the templates.

Following is your code I have done some modification might help you

public class Demo {

     String name = "Europe";

    public String getname() {

        return name;
    }

    public void setname(String name) {

        this.name = name;
        System.out.println(this.name);
    }

    static class innerclass {

        void updatename() {
            Demo o = new Demo();
            o.setname("USA");
        }

    }

    public static void main(String[] args) {
        Demo b = new Demo();
        String a = b.name; // why this error here ? "Syntax error, insert "VariableDeclarators" to complete LocalVariableDeclaration"

        System.out.println(a);

        innerclass i = new innerclass();
        i.updatename();

    }

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

4 Comments

what if outer class's variable is static ?
static things can be accessed
Can you please explain how it can be accessed ?
make this modification static String name = "Europe";
0

Inner static class behives same as normal class:

  • can access static property/method of outer class
  • can't non-access static / methods of outre class directly, they will require an outerclass instance reference to do so.
  • it does not rquire an instance of outer class to be created

It is used mostly in two scenarios:

  • you are creating a group of classes with similar nature/function, and you want to keep them under one 'Napespace'
  • you want to create a private class that will not be visible to anyone, except to outter class (private static inner class). That way you can create interface implementations visible only to your outer class.

Non-static inner class:

  • it requires instance of outer class to be created
  • it can access methods and properties of outer class.

Quote:

...inner classes can access all members of the declaring class, even private members. In fact, the inner class itself is said to be a member of the class; therefore, following the rules of object-oriented engineering, it should have access to all members of the class.

4 Comments

what if outer class's variable is static ?
You can access them
@Beri regarding can't access static / methods of outre class directly, they will require an outerclass instance reference to do so.. I think you can access static methods without instance of outer class.
True, I have forgotten to add non keyword.Thank you for the comment.

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.