Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Funny Output

 
Aseem
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic