Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding Static Declaration

 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

I'm new to Java Technology,please clarify my doubts.Can we declare the class and constructor as a static?
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Helo,
Only a inner class can be defined as static, the top level class cannot be static.(Please correct me if I am wrong)
Also the constructor cannot be static, the compiler will complain in both these cases.

Cheers,
Anjali
 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anjali,

Thanks For Your Reply,

Can we call the static method from constructor?
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
surya.raaj prakash wrote:Can we call the static method from constructor?

Have you try it out. What was the result?
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
yes you can call static methods from constructor. I have written a example here and hope it should make you understand things better.



 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Anjali,
Thank You.

But Why compiler not allowing to declare constructor as a static?
 
Rob Spoor
Sheriff
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the use of adding it? It's already implicitly static since you cannot call it on an existing instance. Instead of allowing it, adding to more confusion, Sun decided that it's simply not possible. They could also have made it required but Sun know that programmer's are inherently lazy, and leaving out the static keyword for each constructor will save a lot of typing.
 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
Thank You Very Much.

But one more doubt,please clarify to me,

But Why The class itself isn't "static",and there no such thing as a static class.But it is Static nested class.
 
surya.raaj prakash
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,
Thank You Very Much.

But one more doubt,please clarify to me,

But Why The class itself isn't "static",and there no such thing as a static class and compiler not allowed.But it is Static nested class.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you think a static top-level class would mean?

John.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic