• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Ways to invoke a method in static inner class

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
class Static Nested is a nested member class (static inner class).
How is it possible to instantiate a static for example
Outer.StaticNested n = o.new StaticNested();
n.print();
Can static classes be instantiated?
I always thought static means no instance?
Thomas.

Full code:
class Outer {
static class StaticNested {
static int value = 10;
static void print() {
System.out.println("value: " + value);
}
}
}
public class Test22 {
public static void main(String[] args) {
/* Step1 */
Outer.StaticNested.print();
/* Step2 */
Outer o = new Outer();
Outer.StaticNested n = o.new StaticNested();
n.print();
/* Step3 */
Outer.StaticNested OSN = new Outer().new StaticNested();
OSN.print();
}
}
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also
Outer.StaticNested osn2 = new Outer.StaticNested();
Because you do not need an object of the outer class for the existence of a nested static one
 
straws are for suckers. tiny ads are for attractive people.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic