| Author |
why System class instance cannot be instatiated
|
Amirtharaj Chinnaraj
Ranch Hand
Joined: Sep 28, 2006
Posts: 215
|
|
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
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
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:
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Amirtharaj Chinnaraj
Ranch Hand
Joined: Sep 28, 2006
Posts: 215
|
|
thanks indeed James amir
|
 |
Sudhakar Reddy Kurakula
Ranch Hand
Joined: Aug 19, 2006
Posts: 42
|
|
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 ]
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Sudhakar Reddy Kurakula: The reason why System class is not instantiated is ,System is a final class...
No, final prevents subclassing.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
isha krishnan
Ranch Hand
Joined: Nov 10, 2008
Posts: 50
|
|
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,
|
 |
Manuel Petermann
Ranch Hand
Joined: Jul 19, 2011
Posts: 175
|
|
I am sorry but its not the reason.
If a class doesn't have a constructor a default one is implicitly applied.
|
Please correct my English.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12921
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
isha krishnan
Ranch Hand
Joined: Nov 10, 2008
Posts: 50
|
|
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
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32667
|
|
|
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
Joined: Nov 10, 2008
Posts: 50
|
|
Thanks Campbell
I got all information
|
 |
 |
|
|
subject: why System class instance cannot be instatiated
|
|
|