File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Funny Output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Funny Output" Watch "Funny Output" New topic
Author

Funny Output

Aseem
Greenhorn

Joined: Jul 26, 2000
Posts: 6
Does anyone why the output of the following piece of code is not as it should be?
import java.util.*;
class Data{
int number;
String Name;
void Data(){
number = 5;
Name = "Friend";
System.out.println("Welcome Friend");
}
}
public class welcome {
public static void main(String[] args){
Data d = new Data();
System.out.println("Welcome to the world of Java" + d.number + " - " + d.Name);
}
}

The out put I get is
Welcome to the world of Java0 - null
thanks in advance
Eva Mendoza
Greenhorn

Joined: Jul 19, 2000
Posts: 2
the output is correct since you didn't actually create
a constructor, you created a method with the same name
as the class. constructors do not return any value.
if you remove 'void', it should run as you expect it to.
Ling Wu
Ranch Hand

Joined: Jul 19, 2000
Posts: 184
I'm a very green greenhorn on Java. So I may not know what I'm saying.
You are right about taking out the keyword void from Data() in order to have the intended results. However, I think Data() that
Aseem created IS a constructor and a constructor is also a method that establishes an object. I don't remember seeing it written anywhere that says that constructors do or do not return value. In this case it obviously does once the keyword void is removed, since void is the keyword that stops any method from returning value. Of course if the variables in the Data class had been declared as private, then the welcome class would not be able to directly use those variables and public get methods would have to be created and called.
Aseem
Greenhorn

Joined: Jul 26, 2000
Posts: 6
Thank you for your help. Removing the void does make it work. But I was wondering if adding void to it makes it an ordinary method of the class, isn't there a clash in the name space for a method to have the same name as the class?
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
Hi, I think a constructor can not have a return type. That is what distinguishes a constructor from a regular method to the compiler. If you have
class Data{
void Data(){
System.out.println("void method");
}
Data(){
System.out.println("no-arg constructor");
}
public static void main(String []s){
Data a = new Data();
a.Data();
}
}
This will give you
no-arg constructor.
void method
Hope this helps.
Ling Wu
Ranch Hand

Joined: Jul 19, 2000
Posts: 184
If I understand you correctly: Because void Data() is a method, not a constructor, in Aseem's code, object d can only get at the variables at the class level, but not the values that are assigned inside the void Data(). That's why the two variables show up as zero and null when called. If the keyword void is removed, then Data() becomes a constructor and the variable values become available when the object is instantiated. Ricky, is that a correct explanation? I would like to get a handle on this. Please help. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Funny Output