Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding methods/constructors

 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,



(1) When i execute this code, no output is produced. Why is it so?
(2) Suppose, in Line 1, i replace with the following code:

Now, when i execute this code "no-arg" is printed. Why is it so?

I kindly request you to resoleve my problem.
Thanks a lot in advance.

Regards,
Vijay
 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear vijaychandran rajagopalan,

You expect the no-arg T() method to automatically execute when you create a new object. But that will NOT happen! This is because the different methods that you have declared are NOT constructors! A constructor does NOT have any return type. NOT EVEN VOID!
So it should be this way:


Notice that I have also removed the "void" from each method signature.

Whereas, in the second case, you are simply calling the method.
[ July 11, 2007: Message edited by: Shyam Prasad Murarka ]
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(1) When i execute this code, no output is produced. Why is it so?

What output were you expecting?

[B](2) Suppose, in Line 1, i replace with the following code:

Now, when i execute this code "no-arg" is printed. Why is it so?[/B]

On line one you call the nullary constructor of class T(). Since you haven't provided a constructor for T, the compiler automatically generates a nullary constructor.

On line two, you use the T instance instantiated on line one to call the overloaded T() method which takes no formal parameters.

The moral of this story is that Java allows you to define methods with the same name as the class name, although I wouldn't recommend it.
[ July 11, 2007: Message edited by: Garrett Rowe ]
 
Vijay Chandran
Ranch Hand
Posts: 186
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Shyam & Garrett

Thanks for your immediate reply. The explanations made by you were excellent and precise for instant understandability.

Now i have understood the working of constructors.


Please resolve this query too:
In the above code, if i replace this code in Line 1:


I have given 10 1's inside T(). This when i execute "int" is printed.
If i enter 11 1's inside T(), t.T(11111111111); it gives "literal out of int range".



If i give 9 2's (or any number between 2 and 9) inside T(), "int" is printed.
But if i give 10 2's inside T() it gives "literal out of int range".

My question is:
Why it is allowing 10 1's in T() and not any other number of length 10 within T()

Kindly resolve this too.

Regards,
Vijay
[ July 11, 2007: Message edited by: vijaychandran rajagopalan ]
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Vijay,

Its because of the integer primitive value's max. range. Why dont you check with the primitives and their allowed-but-maximum values?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic