• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why System class instance cannot be instatiated

 
Amirtharaj Chinnaraj
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • 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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks indeed James

amir
 
Sudhakar Reddy Kurakula
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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 ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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.
 
isha krishnan
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • 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,
 
Manuel Petermann
Ranch Hand
Posts: 177
Hibernate Linux Python
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Campbell

I got all information
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic