Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing values of a given object
    primarykey
    data
    text
    <p>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</p> <pre><code>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()); } } </code></pre> <p>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</p> <pre><code>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()); } } </code></pre> <p>I have been looking here <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html</a> for clues but so far nothing.Is this even possible?.</p> <p>Edit </p> <pre><code>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); } } </code></pre> <p>Error</p> <pre><code>C:\ja&gt;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&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload