Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question about maths class

 
Kartik Mahadevan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Why r we not able to make statements like

Maths a = new Maths();

I assume that constructors r private but is there a way in which we can access them.

Regards
Kartik
 
Thomas Paul Bigbee
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The math class cannot be instantiated, it consists of a bunch of static methods
 
Kartik Mahadevan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
but is it necessary that classes which have a bunch of static methods cannot be initialized.

Regards
M.Kartik
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.lang.Math class has no public constructors, deliberately, simply because it would make no sense to have a java.lang.Math object. There are some other classes like this: java.lang.System is one.

Why do you want to do this?
 
Kartik Mahadevan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sir
I just wanted to check that if i make a class and make it's constuctors private nobody could make objects of that class.

Thanks
Kartik
 
Megs Maquito
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think another advantage of a class having a private constructor and static methods is, it saves memory as it doesn't go on the heap.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to check that if i make a class and make it's constuctors private nobody could make objects of that class


You can do that by just creating such a class of your own, and simple checking if it is possible, no?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic