1.why constructor does not have return type in java?2.why static does not support " this" and "super" keyword in java ?3.which one is better to create a thread by implementing Runnable or By Extending Thread in java?

Showing Answers 1 - 19 of 19 Answers

1.why constructor does not have return type in java?ans: constructor is a special method in java. the purpose of the constructor is to assign values to the instance variable when the object is created for the first time. the name of the constructor is same as the class name. to call the constructor in two ways.with new operator eg: Test t1=new Test(); here Test() is nothing but to call the construtor of class.with newInstanceOf() method in Class class eg: object obj=class.newInstance();2.why static does not support "this" and "super" keyword in java?ans: this and super keyword points to the object reference. in java static variable and method are not the part of the object it is the realted class. objects are stored in heap where as static variableand static methods are stored in static context.3.which one is better to create a thread by implementing runnable interface or extending thread class?ans: creating a thread by implementing Runnable interface is best.1. by extends the thread class in the subclass, forther the subclass doesnot extend any other classes.2. by extending the thread class it overload all the method in the superclass to subclass where our subclass have no use of other mehtod in thread class

  Was this answer useful?  Yes

karthik

  • Jun 9th, 2006
 

thaku kirankumar i got an idea with ur answer

  Was this answer useful?  Yes

vijaya

  • Jun 11th, 2006
 

hi

default return type of constructor is class type only i.e we are using constructor to create objects of that class type.

  Was this answer useful?  Yes

siva kumar reddy

  • Aug 7th, 2006
 

hi

constructor is a special method in java it does not returns anything even void also.actually constructor is used to  intialise the objects with same data or different data.

i have to give breif introduction to static and instance variables

static  variables  are stored in method area .A single copy of static variable is shared by all objects.

instance variables are stored in heap.A seperate copy of instance variable is available in all objects.

that is reason static does not not support super and this keywords in java.

better approach of  thread is creation is thread i.e implemented Runnable interface.

The reason is when we implement interface there is still scope to extend the one or more classes.

cheers guys and gals

urs sweet siva

  Was this answer useful?  Yes

umesh1310

  • Jul 15th, 2009
 

Constructor is used to initialise members of class and does not return any value that is why constructor does not have return type.

  Was this answer useful?  Yes

1)Constructor does not have return type beacuse the return type of constructor is class itself.
2)Static does not support this and super because static is not associated with any object.
3)When we implements Runnable we can extends Any other class also.but when we extends thread we cannot extends any other class.Hence for future proof we should  implements Runnable.

  Was this answer useful?  Yes

1.Why constructor does not have return type in Java?
Because its not a method of any class and the first thing to be executed to get an object of a class [in beginer terms we can say that return type of a constructor is the class object itself]

2.Why static does not support " this" and "super" keyword in Java?
Because static is used at class level and these keywords [this and super] are associated at instance level and hence it doesn't support

3.Which one is better to create a thread by implementing Runnable or By Extending Thread in Java?
It depends on your requirement, if you are intrested (or you have a requirement) in overriding all/most of the methods in Thread class then go ahead and extend that else you want to leave the headache of manageging the thread lifecycle to java creaters, implement the Runnable interface and override only one single method which is run()

Thanks,
Vinay

  Was this answer useful?  Yes

Achal singh rathore

  • Sep 16th, 2011
 

Code
  1.  

  2. class hello

  3. {

  4.    

  5.    void hello(int x)

  6. {

  7.     System.out.print("hi");

  8. }

  9.  

  10. }

  11.  

  12. class main

  13. {

  14.      public static void main(String[] s)

  15.     {

  16.           hello ab=new hello();

  17.    

  18.            ab.hello(2);

  19.    }

  20. }  

  21.  

  22.  


it compiles and runs but it not print "hi" tell me why?

  Was this answer useful?  Yes

Sourav Agrawal

  • Oct 22nd, 2011
 

Constructor does not have any return type .. the answer is ,we don't have ny rights to call constructor , we can't invoke the constructor but at the type of object creation constructor automatically get invoked, where compiler put an return type at itself to the constructor. And the static variable or static mathod are independent , they are not dependent on object, tht thy get invoked with the help of object . NO it doesn't happend static method and variables are free and independent from object , they can be invoked directly . and not necessary to create object in case of static. wheather super and this keyword are realated to object, and in case of static if no object get created then how super and this keyword can work. this is the reason behind this........

  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