• 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

constructors

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen when you attempt to compile and run the following code?
public class Crowle{
public static void main(String argv[]){
Crowle c = new Crowle();
}
void Crowle(){
System.out.println("Greetings from Crowle");
}
}
1) Compilation and output of the string "Greetings from Crowle"
2) Compile time error, constructors may not have a return type
3) Compilation and output of string "void"
4) Compilation and no output at runtime
Hi everybody,
i need some help out here....the answer for this question is choice 4. I didnt get it.....
the same tutorial said....
You may get questions in the exam that have methods with the same name as the class but a return type, such as int or string. Be careful and ensure that any method you assume is a constructor does not have a return type.
if i follow this then my answer would be choice 2 and not 4.
so, if a function with same name of the class is there should i not assume that it is the constructor of the class and hence it should not have return type and hence it is choice 2. Am i wrong? iam confused....expecting a quick discussion
-gayathri
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by gayathri bhushan:
What will happen when you attempt to compile and run the following code?
public class Crowle{
public static void main(String argv[]){
Crowle c = new Crowle();
}
void Crowle(){
System.out.println("Greetings from Crowle");
}
}
1) Compilation and output of the string "Greetings from Crowle"
2) Compile time error, constructors may not have a return type
3) Compilation and output of string "void"
4) Compilation and no output at runtime
Hi everybody,
i need some help out here....the answer for this question is choice 4. I didnt get it.....
the same tutorial said....
You may get questions in the exam that have methods with the same name as the class but a return type, such as int or string. Be careful and ensure that any method you assume is a constructor does not have a return type.
if i follow this then my answer would be choice 2 and not 4.
so, if a function with same name of the class is there should i not assume that it is the constructor of the class and hence it should not have return type and hence it is choice 2. Am i wrong? iam confused....expecting a quick discussion
-gayathri


Hi Gayathri,
Probably it is confusing. Lets think that "void" is also some kind of return type. (a consturctor is a method with same as class noame with NO return type). so when you say
' void Crowle()'
it is not a constructor as it is saying that 'void' is return type. (don't think that when you say 'void' that means no return type. I think java specification for constructor is like that).
So, when you make an instance of Crowle in
Crowle c = new Crowle();
it doesn't call void Crowle ( ). But calls the default constrctor that is given by JVM which is
pulic Crowle () { }
Hope this clears your confusion.
Thanks
Gopikrishna Putty

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Gopi is basically saying is that the method "void Crowle()" has a return type (void), thus it is not considered by the compiler to be a constructor...just a method that happens to have the same name as the class. The only way to get any output would be to add the line "c.Crowle()" in main().
 
gayathri bhushan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didnt get it....

 
Gopi Krishna
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. Direct to point....
Option 4: is correct because void Crowle() is not constructor. And it is not wrong to have a method same as classname but with a return type. It will be treated as any other method in that class but not like a constructor.
Hope this helps.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gayathri,
Let's recap some rules
Class: Name
Constructor: Name/ no return type/no return statement

  • All classes have a constructor.
  • If a constructor is not explicitly declared by the programmer then the JVM provides a default.
  • If even ONE constructor is explicitly declared then no default is provided
  • MUST have the SAME name as the class
  • All constructors implicitly return an object of type class
  • Therefore Cannot have an explicit return type - (Don't declare it of type
    int, String, void, Car, Person or anything)
  • Do not include a return statement in constructor code
  • Can be overloaded

  • Methods:name/ return type/ return statement

    • May also have the same name as the class (this is bad practice and is frowned upon as constructor names usually start with a capital letter whereas method names start with lowercase)
    • MUST have a return type
    • MUST have a return statement corresponding to return type

    • So let's look at different scenarios:
      1. class with no constrcutor declaration; one method

      2. Class with explicit no arg constructor

      3. Class with one constructor

      4. Overloaded constructors

      5. class with constructor and a method with same name

      6. class with no constructor but with a method of same name.
      This is SIMILAR to the class Crowle in your question

      [This message has been edited by Jyotsna Clarkin (edited March 08, 2001).]
      [This message has been edited by Jyotsna Clarkin (edited March 08, 2001).]
 
Jyotsna Clarkin
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Therefore we see that it is LEGAL to declare a method with the same name as the class.
Conversely, if you "accidentally " declare a return type for your constructor then the JVM will NOT recognize that block of code to be constructor (consequently, will not construct the object the way you expect it to). JVM will think that is a method declaration.
Finally here's the answer to your query. Sorry to have rambled on for so long.

1) Compilation and output of the string "Greetings from Crowle"
Wrong because method Crowle() was NEVER invoked
2) Compile time error, constructors may not have a return type
Wrong because Crowle() is a method not a constructor so the compiler will not argue
3) Compilation and output of string "void"
obviously wrong - there is no mechanism to output "void"
4) Compilation and no output at runtime
Correct, because class defintion is fine so will compile but no output (because method Crowle() was not invoked on object c)
You can add the line to the main() to get output
c.Crowle()
regards,
Jyotsna

[This message has been edited by Jyotsna Clarkin (edited March 08, 2001).]
 
gayathri bhushan
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jyotsna and gopi,
Thank you for your explanations...my doubt got crystal clear...thanks again..
-gayathri
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic