-5

If i have a Inner class (not static) example:

public class A {
     int myNumber = 100;

     class B {
     }  

     public static void main(String[] args) {
         A outerObj = new A();
         B innerObj = outerObj.new B();
         System.out.println("i want the access to the variable 
                             myNumber by the innerObject"); 

     }
}

i mean: i would like with the innerObject to reach the outerObject and see the variable myNumber. I can do that just if i'm in a method of the B Class... but i would like everywhere to see the variabile of the outerObj by the inner.... it's possible? if not why?
thanks

3
  • There's no variable numb. Please clarify. Commented Dec 7, 2013 at 21:42
  • @MightyPork He probably means myNumber... Commented Dec 7, 2013 at 21:45
  • yes i mean myNumber sorry Commented Dec 7, 2013 at 21:48

1 Answer 1

1

I assume that you want something like this:

class B
{
   private int getNum()
   {
      return myNumber;
   }

   private void setNum(int x)
   {
      myNumber = x;
   }
}

However, you cannot get access to myNumber from an instance of B, b, by doing b.myNumber. Here's why.

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

4 Comments

yes thi i know, but i dont want enter in the class b to read the variables of the outer obj... i would to read them directly in the main method or in other method but not of the class b
@GiovanniFar Then you could just read from class A? What you're saying doesn't make sense to me. You want to read the members of A without A, but with a nested class that can only be initialized by A? But you don't want to use methods? Why? If so, you need to access from A.
No i mean this: my innerObj points to the outer Obj ok ? there is a way with the innerObj to see the variabiles of the outerObject WITOUHT using the class b so WITHOUT using the class of the innerobj? i would like to know if the innerobj i can see the variable of the outerobj everywhere or just in the class b and why
@GiovanniFar Just in class B, here's why.

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.