1

When we try to instantiate an Inner class (aka: non-static Nested class) in java, say we do that in two cases:

1.in a main method in the same file of the outer Class in which we have two options: (ex:)

public class Test1
{
   class InnerClass
   {

   }
  public static void main(String[] args)
  {
     InnerClass inner = new Test1().new InnerClass();
  }
}

or :

public class Test1
{
   class InnerClass
   {

   }
  public static void main(String[] args)
  {
     Test1.InnerClass inner = new Test1().new InnerClass();
  }
}
  1. In a another class (say different file), then we have the second option only and using the first option requires us (of course) to import the InnerClass..,

Q: could you please explain why do we have the first option (without any import required) in the first case (the main method in the same file)?

Edit:

I guess the answer to the first question is some how related to the core idea of inner classes. but then +Q:

Q: Isn't an inner class a regular-member of an outer class, so if the inner class is not declared static (static nested class) then I suppose it is a non-static member and consequently its reference type, so why are we able to declare it within a static context (static method) ?

Profess Physics
  • 307
  • 4
  • 9
  • 4
    Why would you import in the same file? Anything in the same package has access without imports – Rogue Mar 25 '17 at 15:10
  • good question. this is just an argumentative assumption since I did not get why we have two options in the first case. I mean the inner class is not a static member.., but still we are declaring it + instantiation inside a static context (that is to say the static main method) – Profess Physics Mar 25 '17 at 15:13
  • When you do `new InnerClass()`, it's on an instance variable retrieved via `new Test1()`. When you assign to `Test1.InnerClass` (vs just `InnerClass`, since that's the only difference I'm seeing), there's really no restrictions there. You can even use the FQN if you want: `my.random.package.Test1.InnerClass var = /* etc */;` – Rogue Mar 25 '17 at 15:15
  • So I mean, what is the difference between reference variable and primitive variable.., while I thought that if the inner-class is without the keyword static it has to be non-static.., so how could we put its reference within a static context.., sorry my head is barely functioning after studying intensively today^^. – Profess Physics Mar 25 '17 at 15:29
  • Dude, you created an instance. `new` is allowed in all contexts. Once you've created an instance, and you invoke via that instance, you aren't invoking from a static context, you're invoking from the context of that instance. Think about it, if you couldn't do that, you wouldn't be able to run any program, ever. – Lew Bloch Mar 25 '17 at 15:34
  • Your code won't compile. Please try your code before posting it. – Lew Bloch Mar 25 '17 at 15:34
  • My mistake.., I edited it.., anyway "you aren't invoking from a static context, you're invoking from the context of that instance. " see I could not grab the idea of this line. and still you need to consider two things basically : 1. inner classes are different in the sense they could be declared static unlike the outer ones. – Profess Physics Mar 25 '17 at 15:42
  • 2. let us not talk about the instantiation but rather the declaration only. considering my conceptualization of the thing (inner classes are just regular members, and so their reference types! ? isn't ? so if they are static then the reference type is also and vice versa .., am I wrong dude ^^? ) – Profess Physics Mar 25 '17 at 15:42

2 Answers2

2

Simply, it is because when you instantiate the inner class from a main method outside the class in which the inner class is present, the Java compiler has no way on knowing inside which class lies that inner class. Hence you have to do

Test1.InnerClass innerClassObject = ...

instead of

InnerClass innerClassObject = ...
skb
  • 41
  • 5
0

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

From Oracle Docs : https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

skb
  • 41
  • 5