aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Class Definition Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Class Definition" Watch "Inner Class Definition" New topic
Author

Inner Class Definition

Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172

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

Joined: Jan 15, 2002
Posts: 94
If you create an object of MyThread in another class, then u need declare m as
InnerDemo.MyThread m;


Yet Another SCJP2
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Paul, just take the main() statement and put it into another class like the one below!


[ January 23, 2002: Message edited by: Rajinder Yadav ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
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

Joined: Jan 15, 2002
Posts: 94
actually, it works in a static method like main above.
i would think enclosing class is like a package.
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
Bill,
are you referring to all static methods where this is a requirement?
what about a inner static class?
Bernd Stransky
Ranch Hand

Joined: Nov 20, 2001
Posts: 47
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

Joined: Jan 15, 2002
Posts: 94
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

Joined: Jan 15, 2002
Posts: 94
bearn,
I dont have this book, but I tested my code on my computer. thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Class Definition