Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Inner Class Definition

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you create an object of MyThread in another class, then u need declare m as
InnerDemo.MyThread m;
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bearn,
I dont have this book, but I tested my code on my computer. thanks
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic