Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Regarding methods/constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regarding methods/constructors" Watch "Regarding methods/constructors" New topic
Author

Regarding methods/constructors

Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
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

Joined: May 02, 2005
Posts: 209
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 ]

With Best Regards,
Shyam Prasad Murarka
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
(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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Vijay Chandran
Ranch Hand

Joined: Jan 07, 2007
Posts: 178
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

Joined: Apr 20, 2006
Posts: 3355

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?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regarding methods/constructors