Javastudent
Javastudent

Reputation: 29

How to access outer class variable using inner static class object

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

    }

}

Upvotes: 1

Views: 628

Answers (2)

Nisal Edu
Nisal Edu

Reputation: 7591

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

    }

}

Upvotes: 2

Beri
Beri

Reputation: 11620

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.

Upvotes: 0

Related Questions