• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constructor

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {

public Test () {
System.out.println("Inside the contructor");
return ;
}

public String Test () {
System.out.println("Inside the Method");
return "Welcome";
}

public static void main(String[] args) throws Exception {
Test t = new Test();
System.out.println("End of program "+t.Test());
}
}

Defined a method just as constructor. but still its not giving any compiler error. It gives output as

Inside the contructor
Inside the Method
End of program Welcome
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are allowed to have a method that looks just like a constructor.
It is not usually done because it makes reading code confusing but it is legal.
To identify the constructor vs the method look for the return type.

You will never be able to call the method using new, you have to manually call it
Likewise you can only call the constructor using new or this() or super() never like you would a regular method.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you specify a return type, then it's not a constructor -- it's just a method.
 
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is right, methods must have a return type and constructor must not. Just having return keyword does not mean constructor is returning something.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anikanchan Raut:
... Just having return keyword does not mean constructor is returning something.


That's a good point: If you're careful to avoid unreachable code, "return" can be used to simply exit the method/constructor body.
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Damien Howard:

You will never be able to call the method using new, you have to manually call it
Likewise you can only call the constructor using new or this() or super() never like you would a regular method.




pls explain the above statement in detail pls
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amit taneja:
...pls explain the above statement in detail pls


The keyword "new" indicates the creation of an object, so it must be followed by a constructor call. Thus, you cannot follow "new" with a method call.

Within a constructor body, the first statement can be this(), which calls an overridden constructor for the same class (depending on the arguments provided); or the first statement can be super(), which calls a constructor of the immediate superclass (again depending on the arguments provided).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic