0

Its said that, statics are much comfortable with statics in java. I'm trying to achieve small thing there where I want to change outer's class static variable value using inner static class's instance for that specific instance only. I think its a ideal case. If not please share with me. And moreover all inner classes have access to outer class's members.

So here is my code.

package org;

import org.Outerclass.innerclass;

public class Outerclass {

    static String name = "Europe";

    String getname() {

        return name;
    }

    public void setname(String name) {

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

    void setstaticname() {

        Outerclass.innerclass i = new Outerclass.innerclass();
        i.name = "London"; // Error "name cannot be resolved or is not a field"   ?
        System.out.println(i.name);

    }

    static class innerclass {

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

    }

    public static void main(String[] args) {

        innerclass p = new innerclass();
        System.out.println(p.name);   // Error "name cannot be resolved or is not a field" ?

    }

}

I have tried in two ways and vice versa but same errors. Any suggestions ?

Javastudent
  • 29
  • 10
  • *Its said that, statics are much comfortable with statics in java.* Huh? *I have tried in two ways and vice versa but same errors.* Same as what? – shmosel Oct 29 '17 at 05:19
  • "inner classes have access to" means "inner classes *are allowed to access*". It doesn't say anything about the syntax you need to access the outer class, and in any case, the code that you write to access the other class *is not inside the inner class* so the "have access to" is not applicable. – Erwin Bolwidt Oct 29 '17 at 05:22
  • shmosel, replacing the code with error positions just for trial – Javastudent Oct 29 '17 at 05:23

2 Answers2

0

name is a member of OuterClass. And you are trying to access it using innerclass instance. That's why you are getting this error.

Also what is this + here System.out.println(+p.name); ?

Edit:

From inner class you can access outer class static members just like below:

name = "";

or

Outerclass.name = "";

change +p.name to name or OuterClass.name. + inside System.out.println will give you another compile time error.

static members are not available in object level they are available in class level. static are initialized at compile time where object is created at run time

Shubhendu Pramanik
  • 2,711
  • 2
  • 13
  • 23
  • But I think Inner class do have access to outer class members. No ? And in this case static inner class and the member which I want to access is also static. – Javastudent Oct 29 '17 at 05:26
  • "Also what is this + here System.out.println(+p.name); ?" Its nothing, even I remove its same error exists..!! – Javastudent Oct 29 '17 at 05:30
  • @Javastudent change `+p.name` to `name` or `OuterClass.name`. `+` inside `System.out.println` will give you another compile time error. – Shubhendu Pramanik Oct 29 '17 at 05:33
  • Why I cannot call static variable using static class's object ? – Javastudent Oct 29 '17 at 05:35
  • You can call static variable using static class's object but that's not what static members/ variables are supposed to be used. You can call static members of the outclass simply by accessing it through class references. So ```OuterClass.name``` would be ok – Infamous Oct 29 '17 at 05:39
  • @Javastudent I have explained in the updated version. check it. – Shubhendu Pramanik Oct 29 '17 at 05:40
  • @ShubhenduPramanik, tell me one thing..when ever the code compiles the entry point is from main static method. But once its entered from there, Does it loads every static thing in whole code before it starts creating objects ? – Javastudent Oct 29 '17 at 05:47
  • @Javastudent No. `main` method does not execute at compile time. At compile time only static members are initialized. `main` method is used to start execution from outside. Read more here https://stackoverflow.com/questions/146576/why-is-the-java-main-method-static – Shubhendu Pramanik Oct 29 '17 at 05:58
0

i.name should be Outerclass.name since name is a member of Outerclass and not innerclass

in your code, i is an instance of innerclass.

Ammar Samater
  • 529
  • 2
  • 7
  • 24