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

Constructors/Strings HELP

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it is early ...
But, i am
Why does the following program produce 'null'.
public class testA {
String s;
public static void main(String argv[]){
testA m = new testA();
m.go();
} // end of main
void testA() {
s = "Constructor";
} // end of constructor
void go() {
System.out.println(s);
} // end of go method
} // end of class testA
Help Monty6,
 
Ankur Gupta
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because "void testA()" is taken to be a method as it has a return type, instead of a constructor.
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The problem lies with the fact that a default no-arguement constructor is used (not the one you THINK you've used. )Constructors cannot be void. Therefore s is never initialised.
Hope this helps
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what your saying is that the JVM is giving me a default
constructor. Because void testA() is a method.
Question: What is the signature of a constructor?
Still
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank-you, now I feel silly and .
I at myself ... for forgeting the basic's
Monty
p.s. why is the server running so slow?

 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The signature of a constructor consisted of an access modifer (or not if it will have package scope) and the name of the class and the arguements.
eg.

[This message has been edited by Angela Poynton (edited June 22, 2000).]
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem.
I often find that I often get so caught up in the complexities of java that the basics can easily be overlooked.
I have to admit, it had me stumped for a while!!
 
Arijit Kundu
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a constructor cannot have a return type so void is not allowed .
it creates another method in the same class with a return type of void and the same name as the class .

------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic