File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors/Strings HELP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors/Strings HELP" Watch "Constructors/Strings HELP" New topic
Author

Constructors/Strings HELP

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 13, 2000
Posts: 66
Because "void testA()" is taken to be a method as it has a return type, instead of a constructor.
Angela Poynton
Ranch Hand

Joined: Mar 02, 2000
Posts: 3143

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


Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Mar 02, 2000
Posts: 3143
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

Joined: Mar 02, 2000
Posts: 3143
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

Joined: Jan 19, 2000
Posts: 19
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 .

------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors/Strings HELP