• 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

why System class instance cannot be instatiated

 
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys

when i go through the API java.lang.System

i found that System class cannot be instantiated

my questios is why the System class cannot be instantiated.

even though it is not having the constructor jvm supplies

a default no-Arg-Constructor

amir
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your "why" question could have two answers ...

Why the designers decided not to let you make an instance ... They put all the functionality you need in static methods and variables so no instance is necessary.

Why you can't make one against their wishes ... They made the constructor private. This is a common way to build classes that are meant to be all static or that want to control instantiation, for example the Singleton pattern.

You can download the source for the library and look up things like this:
 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks indeed James

amir
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason why System class is not instantiated is ,System class has a private constructor().


Cheers
Sudhakar
[ December 31, 2006: Message edited by: Sudhakar Reddy Kurakula ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sudhakar Reddy Kurakula:
The reason why System class is not instantiated is ,System is a final class...


No, final prevents subclassing.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

The reason why System class is not instantiated is ,System class has a private constructor().



System class has no constructor that's why it can not be instantiated not even a private constructor. As we see in string api,there is no constructor defined. Please let me know if its the correct reason.

Thanks,
 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry but its not the reason.
If a class doesn't have a constructor a default one is implicitly applied.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

isha krishnan wrote:System class has no constructor that's why it can not be instantiated not even a private constructor.


That is not correct. The System class does have a private constructor, which is not mentioned in the Javadoc documentation. The private constructor is what prevents you from instantiating the class. If a class has no constructor, a public constructor which does not take arguments is automatically added by the Java compiler. So if a class has no constructor, it can still be instantiated.

You can lookup the source code for class System in the file src.zip which is in your JDK installation directory. There you'll see that it has a private constructor, as Stan James already showed above.

isha krishnan wrote:As we see in string api,there is no constructor defined.


Class String has several constructors.
 
isha krishnan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The System class does have a private constructor, which is not mentioned in the Javadoc documentation



So why is it not mentioned in JavaDoc?It should be there i guess so
Yes that's perfectly correct that if no constructor is given,its default constructor is called.I was wrong here.

Also in quote

As we see in string api,there is no constructor defined.



I did mistake in writing, i wanted to write "System" instead of String.

Thanks
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is normal for private members and constructors not to appear in the API documentation. The javadoc tool usually does not produce visible output for private members. Anybody reading about uninstantiable classes would think of this (or similar).
 
isha krishnan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell

I got all information
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic