Can we make construtor STATIC?

Questions by ansh_ranjan   answers by ansh_ranjan

Showing Answers 1 - 18 of 18 Answers

krishna

  • May 1st, 2006
 

bugger, what r u asking why do u want to make a constructor static.

constructor is used when u instantiate a class, what will do with static constructor because static methods are called when u don't want to instantiate.

use your head x-(

  Was this answer useful?  Yes

sekhar

  • May 8th, 2006
 

hi krishna, u are using ur head but that is not in the proper way.u told ""static methods are called when u don't want to instantiate"".but that is not the "main" use of static in this case.static is used when u want to define a variable or a method that is shared by all objects of a class. but the purpose of the constructor is to initialise a "PERTICULAR" object.and the purpose of static is to share the information across all the objects.here we are getting conflict.so we can not use static to a constructor.

  Was this answer useful?  Yes

tejaswee

  • May 16th, 2006
 

Dear,

You cannot make a constructor as STATIC, because if you use any access modifier in constructor compiler interprets it to be a method and expects a return type this class will not compile until you specify a return type, since compiler thinks it to be a normal method not a constructor.

Cheer's

Tejaswee P

  Was this answer useful?  Yes

bliss

  • May 17th, 2006
 

Hey Tejaswee,

You can use private constructors. So how can you say you cannot use access modifiers for constructors ?

  Was this answer useful?  Yes

hvignesh

  • May 17th, 2006
 

bliss.. tejeswee is right... the compiler will think that it is some method and not consider as a constructor.. as she said u cant have a method with static modifier and withour return type.. the compiler will yell at u.. saying no return type specified.. try it out with an eg:eg:class test{ public test() { System.out.println("this is constructor"); test(); } public static void test() // this will thro error.. saying that static should not be mentioned..if with return type it is a legal method.. { System.out.println("a method with the class name"); } } class rr { public static void main(String ar[]) { test obj=new test(); }}

  Was this answer useful?  Yes

Seetha Ram Janapala

  • May 22nd, 2006
 

The compiler will give the following error if you use ?static? keyword in constructor:

MyClass.java:3: modifier static not allowed here

        public static MyClass()

                                ^

  Was this answer useful?  Yes

Yugal kishore saahu

  • Jun 20th, 2006
 

Constructor can only have private, protected & public Access modifiers, & default constructor or constructor without modifier have class modifiers...

  Was this answer useful?  Yes

tafazzul hasan

  • Feb 20th, 2007
 


We can not use STATIC with a Constructor. We can use only Public,Private,Protected and Default modifiers with a Constructor.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions