• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor question

 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Swapan Mazumdar
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much to ALL...at last i understand it ....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic