This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Class Definition

 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

my question relates to:
MyThread m = new InnerDemo().new MyThread()
I assume that we do not need:
InnerDemo.MyThread m = new InnerDemo().new MyThread()
because we are already w/in the outerclass. Could someone please post an example which includes the use of:
InnerDemo.MyThread m = new InnerDemo().new MyThread()
I already have examples of others just not this one.
TIA
 
bill williams
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you create an object of MyThread in another class, then u need declare m as
InnerDemo.MyThread m;
 
Rajinder Yadav
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, just take the main() statement and put it into another class like the one below!


[ January 23, 2002: Message edited by: Rajinder Yadav ]
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
When you create an inner class from outside the enclosing class OR from within a static method - you need to use InnerDemo.Mythread m = ...
Only when you are within an instance method of the enclosing class can you omit the innerDemo reference.
So for your example, it should not work as main is a static method.
That is my understanding.
-Bernd
 
bill williams
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually, it works in a static method like main above.
i would think enclosing class is like a package.
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill,
are you referring to all static methods where this is a requirement?
what about a inner static class?
 
Bernd Stransky
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by bill williams:
actually, it works in a static method like main above.
i would think enclosing class is like a package.

I got the info from Mike Meyers Book "Java 2 Passport Certification", chapter 3, page 110.
-Bernd
 
bill williams
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bernd,
<quote>
When you create an inner class from outside the enclosing class OR from within <i>a static method </i>- you need to use InnerDemo.Mythread m = ..
</quote>
you dont need use InnerDemo.Mythread m=... in a static method as long as it is IN the enclosing class. the following example works fine:
<code>
class Test1{
class A{
final static int a=0;
final int getI(){return a;}
}
private static void main(String[] args){
//A a = new Test1().new A();
//A a = new Test1().getA();
A a = getA();
System.out.println("a = " + a);
}
public static A getA(){
A a = new Test1().new A();
return a;
}
}

</code>
 
bill williams
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bearn,
I dont have this book, but I tested my code on my computer. thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic