• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Doubts on the constructors

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai iam Naveen Kumar write now iam taking suncertification scjp1.5

i have a doubt in the constuctors as we know that constructors donot have return type but i have used return type in constructor and it is still compiling it is not giving error, can anybody please tell me whether it is using default constuctor because its giving the value as '0' for both x and y but it should give compilation error when i run this program

here is the program and i have complied it in jdk1.5 version


public class testconstructor{

int x,y;

void testconstructor(){

x=10;
y=20;
}

void printme(){

System.out.println("x value "+x+"y value"+y);
}

public static void main(String ar[]){

testconstructor con1=new testconstructor();

con1.printme();
}
}
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Naveen,

your class name is testconstructor.

A constructor must have the same name and no return type, e.g.:



You wrote a method that had the same name as the class, but no constructor. It is no constructur because it has a return type, even when the return type is void. This was legal, but the method was not called as you constructed an object of the class:



So x and y are still 0.

Hope it helps,
Peter
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, some of the exam question are fooling us with methods that in the first place are looking like a constructor but aren't.
So set it in bold type: constructors don't have a return type.


Something else, "Naveen KumarBS":

There aren't many rules to follow on the ranch, but one is about the use of propper display names.

Your name does not follow this rule. The main reasons why and a link how to change yours you'll find here:
http://www.javaranch.com/name.jsp

So, could you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.


I'm writing this because I am one of the moderators of this forum.


Yours,
Bu.
 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya, it is a method in this program having the same name as of class & it is not invoked in the program so does not set the value.That is why printme method shows default value of instance variables.
 
reply
    Bookmark Topic Watch Topic
  • New Topic