• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Declaration and Access confusion...

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The private access modifer cannot not be used as a class modifer correct?
Given a "private class with a public variable," will result in a complier error correct? This is the case for top-level and nested classes as well correct?
Thanks,
Tim
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Timothy Stone:
The private access modifer cannot not be used as a class modifer correct?
Given a "private class with a public variable," will result in a complier error correct? This is the case for top-level and nested classes as well correct?
Thanks,
Tim


Hi Timothy,
It is true that Access modifier 'private' is not applicable to classes.
The compiler complains saying that - "the class cannot be private. and package members are always accessible within the current package."
And according to a table 7.1,pg#225,in Khalid's book
He mentions that 'Top-level nested classes' and 'non-static inner classes' can be specified with any access modifiers.
But local classes(static and non-static), and Anonymous classes( static and non-static) cannot use any of the access modifiers.
HIH,
chandrashekar!
 
Timothy Stone
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So... when defined in seperate source files:
One.java

and
Two.java (non-static local class)

won't compile?

But
Three.java ( static member, or 'top-level nested," classes )

will compile?
[This message has been edited by Timothy Stone (edited October 04, 2001).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Timothy,
A top-level class can only be declared with the public access modifier or no access modifier (default access).
A static nested class or an inner class can be declared private, protected or public or have no access modiifer.
The following compiles without error:

A private class can have a public variable but the variable will not be visible outside the class as it's access is subordinate to the class access.
For your examples,
One.java - won't compile. A top-level class can only be public or have no access modifier.
Two.java - will compile without a problem. An inner class can be private
Three.java - won't compile. You've declared three top-level classes. Only <code>public class Three</code> is a legal declaration.
The easiest way to check for such things is to compile them yourself
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Timothy Stone
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A top-level class can only be declared with the public access modifier or no access modifier (default access).
A static nested class or an inner class can be declared private, protected or public or have no access modiifer.


I think I was looking for the concise rule. Thanks! And yes... I saw the error of my code examples but too late for an edit. And compiling is the simpliest way. But sometimes you can write examples all day to test, but never get to the bottom of what is making the rule tick.
I was having a lot of confusion on the matter.
Thanks!
Tim
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jane Griscti:

A [b]static nested class
or an inner class can be declared private, protected or public or have no access modiifer.
[/B]


Hi Jane,
Can you please explain what a "nested class" is?

 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A nested class is one that is not member of a package, but it was declared within a another class or interface. An inner class is a nested one that is not static.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose,


A nested class is one that is not member of a package


The question of a 'package' doesn't really come into it.
A top-level class is not enclosed within the declaration of another class.

A nested class is any class enclosed within the declaration of another class.

In the above, both NestedA and NestedB are nested classes BUT only NestedB is an inner class.
For a description of top-level and nested classes see JLS §8


A nested class is any class whose declaration occurs within the
body of another class or interface. A top level class is a class that is not a nested class.


For a description of Inner classes see JLS §8.1.2


An inner class is a nested class that is not explicitly or
implicitly declared static.


Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Jane
Does defining a nested class as not member of a package, because its member of a class, presents any other problem besides semantics ones?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic