Accessing values of a given object

I am wondering why i have to deal with two types of arguments;that of a constructor and that of a method.For instance i have this simple class that adds two numbers

class Calc{
private int x = 6;
private int y;
private char z = 'z';

public int getx(){
return x;
}
public char selfrecur(){
return this.z;
}
public int add(int one,int two){
return one + two;
}

public static void main(String[] args) {
Calc gx = new Calc();
System.out.println(gx.x);
System.out.println(gx.add(44,3));
System.out.println(gx.selfrecur());
}
}

That works,and wow,wasn't that great.Now,i have this idea of having the constructor provide the arguments and the function's work will be to do the heavy computations.For instance in my class Kalc

class Kalc{
//** This example won't work **
private int x;
private int y;
private int z;

public Kalc(int v1,int v2,int v3){
this.x = v1;
this.y = v2;
this.z = v3;

}
public int add(){
return newObject.x + newObject.y + newObject.z;
//Gets the values of a new object and add them up
}
public int multiply(){
return newObject.x * newObject.y * newObject.z;
//Gets the values of a new object and multiply them
}

public static void main(String[] args) {
Kalc k = new Kalc(4,5,6);
System.out.println(k.add());
System.out.println(k.multiply());
}
}

I have been looking here http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html for clues but so far nothing.Is this even possible?.

Edit

class Kalc{
private int x;
private int y;
private int z;

public Kalc(int v1,int v2,int v3){
this.x = v1;
this.y = v2;
this.z = v3;
}
public int add(){
return this.x + this.y + this.z;
}

public static void main(String[] args) {
Kalc k = new Kalc(4,5,6);
System.out.println(k.add);
}
}

Error

C:\ja>javac Kalc.java
Kalc.java:17: error: cannot find symbol
System.out.println(k.add);
                    ^
  symbol:   variable add
  location: variable k of type Kalc
1 error

C:\ja>

Upvotes: 0

Views: 108

Answers (4)

BobTheBuilder
BobTheBuilder

Reputation: 19304

Use this key word:

public int add(){
    return this.x + this.y + this.z;
}

You can use this key word inside non-static methods too.

About your edit: add is a function (and not a member) of class Kalc so you can call it as a function only:

System.out.println(k.add());

Upvotes: 1

Mohamed AbdElRazek
Mohamed AbdElRazek

Reputation: 1684

I think you need to print :

System.out.println(k.add());

Instead of :

System.out.println(k.add);

as in the second case the compiler show k.add as add variable but in the first case add() the compiler show add() as a function which you define in Kalc Class

Upvotes: 0

Aravind Yarram
Aravind Yarram

Reputation: 80192

You can do the below

class Kalc{
    private int x;
    private int y;
    private int z;

    public Kalc(int v1,int v2,int v3)
    {
        this.x = v1;
        this.y = v2;
        this.z = v3;
    }
    public int add(){
        return x+y+z;
    }
    public int multiply(){
        return x*y*z;
    }
    public static void main(String[] args) {
        Kalc k = new Kalc(4,5,6);
        System.out.println(k.add());
        System.out.println(k.multiply());
    }
}

Upvotes: 1

scottb
scottb

Reputation: 10084

What is newObject?

You have instantiated an object with prescribed values. If you want to add them with an instance method, try this

return this.x + this.y + this.z;

Upvotes: 0

Related Questions