Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Can a constructor return?

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can a constructor return? I did, but i am not sure whether it is treated as a constructor or a member method.

public class finalTest {

int finalTest() {
return 1;
}

String finalTest(String name) {
return "hello";
}

public static void main( String args[] ) {
finalTest f = new finalTest();
System.out.println(f.finalTest());
System.out.println(f.finalTest("test"));
}
}

Output
------
1
hello
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Constructors can never return a value; the methods you provided are NOT considered constructors because they return int and String respectively.

I modified your code a little bit, now notice that if the method finalTest() on line 1 was a constructor then it will output �Hello World� upon the creation of the object on line 2. But it doesn�t, this means that it is treated as a method and not as a constructor.


[ July 26, 2004: Message edited by: Vicken Karaoghlanian ]
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

A constructor cannot have a return type. That rule stands even if Java were to be spelt as 'avaj'.

If, for whatever reason, you have a method that matches the name of the class with a return type specified, Java will consider it to be a normal method (I'd thank Java's engineers to be so kind).

Looking into your code, you will see that 1 is printed when you did a



and not when you did a



Hope that helps!!!


Cheers
 
Kitty Dayal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot,

I got it nailed in.
 
Kitty Dayal
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about this?

public class finalTest {

void finalTest() {
System.out.println( "Hello Constructor" );
}

public static void main( String args[] ) {
System.out.println( new finalTest() );
}

}

It prints
finalTest@108786b

Is it a default constructor defined and why did it print junk

Thanks.
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


void finalTest() {
System.out.println( "Hello Constructor" );
}



This is not a constructor. Constructors don't have any return type. If you put a return type then it becomes a normal method. So the the code above is a method. Since this class dosn't have a default constructor, the compiler creates a default constructor.

System.out.println( new finalTest() );


Here you have put new keyword inside a print method. You have to remember that when you create an object it returns a reference to that object. In this case you have given it in a print method, so it prints the referrence variable. So what appears as junk is actually a referrence.

Hope its clear

Sanyev
 
reply
    Bookmark Topic Watch Topic
  • New Topic