problem of the output
i try this code :
publicclass calter{
privateint result;
publicint calter(int result){
result = 2 + 2;
return result;
}
publicstaticvoid main(String[] args){
calter ok =new calter();
System.out.println("The result is " + ok.result);
}
}
but why the result is 0 not 4?
[946 byte] By [
goldnicka] at [2007-11-27 11:22:24]

The confusion lies in that you have called the method and variable the same name. Try renaming your method and when you make the method call, place brackets () afterwards.
method();
Ack. You have called the method and the parameter the same name. So you need to call the method which you are not doing and get rid of the parameter as it is not really needed.
Message was edited by:
flounder
Third time lucky. The parameter and the member variable have the same name. I need a break.
Message was edited by:
flounder
public class Foo {
private int result; // not really needed
public int bar() { // parameter not needed
return 2 + 2;
}
public static void main(String[] args){
Foo ok = new Foo();
System.out.println("The result is " + ok.bar());
}
}
except having the same name of the class and the method...
you'll always get 0, because the vairable result
inside the method will be born there and die there inside the method..
so when you're calling the global result what you're having is the default value of a new int which is 0;
to solve this out your code should be like this:
public class Calter{
private int result;
public static int getResult(int result){
result = 2 + 2;
return result;
}
public static void main(String[] args){
System.out.println("The result is " + getResult());
}
}
this time you'll have four..
make sure you're awake of such problems, because those are one of the games for the programmer exam (if you haven't applied for)..
hope it helped...(^_^);
thank a lot i know what is my problem now
but i have ather question
public class calter{
private int result;
private int a;
private int b;
public void calter(int a, int b){
this.a = a;
this.b = b;
}
public int getresult(){
result = this.a + this.b;
return result;
}
public static void main(String[] args){
calter ok = new calter();
ok.calter(2, 3);
System.out.println("The result is " + ok.getresult());
}
}
why i assign the value for the follow code is not work?
calter ok = new calter(2,3) but this =>>> ok.calter(2,3) (is work.)
calter ok = new calter()
only creates a new calter object, it does not call the calter() method in your calter class
you could also do this
public class Calter {
private int a;
private int b;
public Calter (int a, int b){
this.a = a;
this.b = b;
}
public int getResults() {
return a + b;
}
public static void main (String[] args) {
Calter ok = new Calter(2,3);
System.out.println("result :" + ok.getResult());
}
}
which uses a constructor with arguments to create a new Calter object, and at the same time initialise the a and b variables with the passed arguments
remove the void in calter().
here's your code:
public class calter{
private int result;
private int a;
private int b;
public calter(int a, int b){
this.a = a;
this.b = b;
}
public int getresult(){
result = this.a + this.b;
return result;
}
public static void main(String[] args){
calter ok = new calter(2,3);
System.out.println("The result is " + ok.getresult());
}
}
it dosen't work for the following code
public class calter{
private int result;
private int a;
private int b;
public calter(){
this.a = a;
this.b = b;
}
public int getresult(){
result = a + b;
return result;
}
public static void main(String[] args){
calter ok = new calter(2,3);
System.out.println("The result is " + ok.getresult());
System.out.println("The result is " + ok.result);
}
}
because your constructor
public calter() {
this.a = a;
this.b = b;
}
is missing its arguments, it should look like:
public calter(int a, int b) {
this.a = a;
this.b = b;
}
calter ok = new calter(2,3);
first of all the code is not compileable as this constructor type is not there in base class so pls put a parametrised constructor or remove the parameters while craeting class instance.