• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

private static nested class

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,
I'm practicing nested classes. I've written this simple class:

public class OuterClass {
private static class InnerClass {
private InnerClass() {
System.out.println("in constructor" }
}
}
}
and another simple class to test:
public class TestClass {
public static void main(String[] args) {
OuterClass.InnerClass in= new OuterClass.InnerClass();
}
}
All classes compile and main method runs with no problem. Now I wonder what is the meaning of the private modifier here ??
Thanks
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wait a minute , this doesn't compile!!...
You could probably be watching another source code..

??? Strange don't you think ?

Originally posted by Tareq Shaheen:
Hi everyone,
I'm practicing nested classes. I've written this simple class:

public class OuterClass {
private static class InnerClass {
private InnerClass() {
System.out.println("in constructor" }
}
}
}
and another simple class to test:
public class TestClass {
public static void main(String[] args) {
OuterClass.InnerClass in= new OuterClass.InnerClass();
}
}
All classes compile and main method runs with no problem. Now I wonder what is the meaning of the private modifier here ??
Thanks

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you may have noticed from the above responce the private keyword in the inner class is the access modifier. Thus, TestClass's methods have no access to InnerClass. Hovever, OuterClass could use and modify the InnerClass.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now I wonder what is the meaning of the private modifier here ??


If you like, you can imagine your static InnerClass as some sort of "static variable" of OuterClass. The analogy is not so farfetched; given a static variable
static int someStaticVariable;
defined in OuterClass, it only makes sense to access someStaticVariable outside of OuterClass's class scope as OuterClass.someStaticVariable.
Now, this InnerClass can instantiate objects. How accessible are these InnerClass objects to outside classes? These are defined by the access modifiers you set for your inner class declaration. Since you've declared InnerClass as private, outside classes like TestClass has a much of a chance accessing InnerClass objects as accessing OuterClass.someStaticVariable, if you declared
private static int someStaticVariable;
in your OuterClass declaration.
-anthony
 
Tareq Shaheen
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'm using Oracle's JDeveloper 3.2 ( jdk1.2.2). The code I posted compiles and runs with no errors in Jdeveloper.
However, I tested this on the command line ( using javac directly ) and it does not compile ! .
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic