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 Constructor question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor question " Watch "Constructor question " New topic
Author

Constructor question

Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
when i have sample code like following :


why the compiler always prompt for "constructor Animal(string) expected" error ? why do we need a "Animal(String) constructor" ? thank you
[ January 16, 2005: Message edited by: Alvin chew ]
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
Because
this( callStaticFn() );
refers to a constructor in this object, that takes a string (which is what the callStaticFn() function returns).
To call a method of this object, use the following notation:
this.methodName( argumentList );
Andris
P.S. you will also need to correct the "this.name" expressions, since in this way you are trying to refer to a memeber of this particular object from the static context, which is not possible.
Andris Jekabsons
Ranch Hand

Joined: Jan 20, 2004
Posts: 82
P.P.S. When calling the "callStaticFn()" function from the constructor, you don't actually need to use the "this" keyword, because it refers to a static method - a method of the class, not an object.
Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
thanks, Andris for reply , but i still have doubts on it ..

1) as we know callStaticFn() function return string ? i still can't get the idea behind why there is a need for constructor with string? as result we need "Animal(String){}"

2) as static method only be able to get static variable, why callStaticFn() can get instance varialbe "name" ?

thank you again for tolerance on my stupid question
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
yeah dude u got it right!!!,
U will get a compiler error for this program, under method callStaticFn() as u r accessing instance variable inside a static method
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522

as we know callStaticFn() function return string ? i still can't get the idea behind why there is a need for constructor with string? as result we need "Animal(String){}"


You are confusing the difference between this.callStaticFn() and this(callStaticFn()). Remember they are not the same. The first one is used to explicitly reference instance variables/methods in the current object, it is equivalent to callStaticFn()

this() on the other is used to invoke another constructor in the same class, and since callStaticFn() method is returning a string, then you are obliged to provide a constructor that accepts string as a parameter.


as static method only be able to get static variable, why callStaticFn() can get instance variable "name" ?


It can't. Compile your code and again and scroll down to the bottom error message and you'll see a compile error saying: Non-static variable cannot be referenced from a static context. To fix this problem, simply mark the name variable as static, or move its declaration inside the callStaticFn method.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
Swapan Mazumdar
Ranch Hand

Joined: Jul 23, 2003
Posts: 83
Hi Alvin,

Let me attempt to break the problem into smaller pieces.
Scenario 1:
public class Animal {
String name;
Animal(){
this(callStaticFn());
}
static String callStaticFn(){
name = "static method called ..";
return name;
}
}
Here this(callStaticFn()); will try to locate a constructor Animal(String param). But failing to do so will throw a compile time error like "cannot resolve symbol this(callStaticFn());"

Scenario 1:
public class Animal {
String name;
Animal(){
this(callStaticFn());
}
Animal(String param){
}
static String callStaticFn(){
name = "static method called ..";
return name;
}
}
In this scenario the compiler complains that non-static variable name cannot be referenced from static context at
//name = "static method called ..";

According to my understanding Andris and (you) Alvin both are right if the points are jointed together.

Cheers guys
Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
thank you very much to ALL...at last i understand it ....
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Constructor question