• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

nested static class

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
It is possible to instantiate an object of a nested static class. Can anyone explain the rationale behind this?
Shashi
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is yes. It can be instantiated by
"Outer.Inner X = (new Outer()).new Inner()" if I am not mistaken (please correct me if I'm wrong).
About it's usefulness.. Unfortunately the number of issues regarding static inner classes do not represent their importance. I have not found a lot of usefulness for static inner classes. Their main points of interests are that they have access to the static methods of their surrounding class. Even the Java Langauge Specification fails to provide any ideas. It does state that static inner classses can be used to 'create you own naming scheme'. I presume they have been invented to extend the exam with a few questions
 
Chris Meijers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is odd.. When using the following works:
Outer o1 = new Outer();
Outer.Inner i1 = o1.new Inner();
o1=null
Then the constructon: Outer.Inner i1=(new Outer()).new Inner() seems identical to me. I believe that every reference can be sustituted by the 'new' call itself.
Using a temp oject o1 should not make any difference
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose that no instance of the enclosing class is required to create an instance of the static inner class.But you have to give the qualified name as in Gautams first example above.
Also
Outer o1 = new Outer();
Outer.Inner i1 = o1.new Inner();
Works just like you can access static variable of a class through either an instance (or using the class name). In this second case, when you create instance o1, the compiler doesnot know why you are making it, but in case you are creating the outer instance inline with the inner instance, the compiler can tell that the instance is not required ( and cannot be used anywhere else in the program ). I hope i am clear enough.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Chris,considery the following code,it compile and run without problem:
class Sample {
static class LittleSample {
static String str = "stout";
LittleSample() {
System.out.println(str);
}
}
}
public class SampleTest{
public static void main( String arg[] ) {
System.out.println("I'm a little sample, short and ... ");
Sample.LittleSample b = (new Sample()).new LittleSample();
}
}
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic