Login with  Log in with facebook
Hiring Manager? SIGN UP HERE
1

what's wrong with following code...

Class A{

A(){

}

int A(){

//some code....

}

}

Ashish Bhargawa
08/12/2011 at 10:59
8 Answers
1
2

Only constructors can have method name same as class name and constructor can not have a return value.

Rishi Yadav
08/12/2011 at 23:15

2
0

This code is correct.

There is not problem it will work correctly.

first will work as default constructor.

And second will work as a method.

Abhendra Singh
08/19/2011 at 06:40

3
0

We can write our own methods with any name(class name).First jvm will execute the A class constuctor and as well as instance method with object reference.

Jampani Narayanarao
08/19/2011 at 10:44

4
0

The method

int A() {

// some code

}

must have a return statement, to return an int value.

otherwise there is nothing wrong.

 

Pravin Jain
08/19/2011 at 12:44

5
0

That code is leagal, but it doesn't mean it is preferred.

Method name having the class name is not a standard, so when other program looks at the code he might mistaken that for constructor.

Maniganda Prakash
08/19/2011 at 21:22

6
0

here method

int A(){

-----

some code

------

}

there is nothing wrong coz it contains return type as integer

Mukesh Kumar
08/20/2011 at 04:26

7
0

The same piece of code will not compile. why because ,as the method () has return type int it should have a return statement that returns int value.Other than this every thing seems good.

Satyanarayana Kakitha
08/21/2011 at 04:00

8
0

but i think that we can't have two mathods with same signature in a class.

so it will not work....

Ashish Bhargawa
08/22/2011 at 11:52

If you want to post any answer to this forum then you need to log in.
Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *
Details