Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

visibility of the default no-argument constructor?

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

Anyone knows what is the visibility of the default no-argument constructor that I am getting from the compiler....? (private/package/protected/public....)
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can check it by yourself.
Just create a Tester class (in which you don't provide your own no-arg constructor) and try to instantiate Tester from different scopes like:
(1)Same class
(2)Same package subclass
(3)Same package non-subclass
(4)Different package subclass
(5)Different package non-subclass

Do reply if you check this or find the answer buy some other means
Thanks..
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.8.9
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not know a class can be declared as protected or private?



Till this day I thought it can only be declared as public or package/friendly. Is this something new?

And what can you do with a private class?
[ November 18, 2007: Message edited by: Joseph Sweet ]
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Joseph Sweet:
I did not know a class can be declared as protected or private?



Till this day I thought it can only be declared as public or package/friendly. Is this something new?


Only inner classes can be protected or private.


Think about why top classes can only be public or package-visible:

A protected class can be accessed by classes in the same package and subclasses only. However, a subclass outside the package cannot access the class until it already is a subclass. But because it cannot find the class at this time it cannot become a subclass. Therefore a protected class makes no sense.

A private class cannot be accessed by any class but itself. Therefore, the class would be useless to any other class, and therefore useless altogether.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:

A private class cannot be accessed by any class but itself. Therefore, the class would be useless to any other class, and therefore useless altogether.



The nirvana of total encapsulation...
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys.

So, static nested classes can only have package or public visibility?
 
Rob Spoor
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

Top level classes (not nested) can only have package or public visibility.
All nested classes, either static or not, can have any visibility.
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well this is quite weird because,

On one hand you say, let's regard static nested classes as mere fields, hence we can let them have each one of the four modifiers (private, package, protected, public).

But then, on the other hand, if they are really like fields, then they are static fields, so how come you can instantiate them like this:



And then, the fact that you can really instantiate them goes well with the fact that they are not called inner. Hence in many places they are described as basically top-level classes that have been inserted into the outer class just for convenience (well sort of... they have some constraints and abilities that top level classes don't have).
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Joseph]: So, static nested classes can only have package or public visibility?

No. This makes me think that a communication error has occurred, and there are two likely sources, which I think should be addressed:

[Rob]: Only inner classes can be protected or private.

More correctly: only nested classes can be protected or private. Nested classes include inner classes and static nested classes; by definition, inner classes are not static. However people often use "inner" and "nested" interchangeably, which can create confusion.

[Joseph]: Hence in many places they are described as basically top-level classes

Yes, unfortunately Sun rather stupidly used the term "top-level nested class" to describe static nested classes when they were first introduced to Java. In 2000 this odious, nonsensical term was removed from the JLS - or more precisely, it was never in the JLS, but a new JLS came out which superseded the badly-named and nonsensical "Inner Classes Specification" which had previously attempted to define nested classes. Since then, officially the term "top-level nested class" has been a logical contradiction (as it should have been all along). Unfortunately many other sources were slow to respond to this change, and you can still find books and people using the old terminology. Please ignore them. All classes are either top-level or nested, but never both. Static nested classes have some things in common with top-level classes, and other things in common with inner classes, but they are neither top-level nor inner. They're in between.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic