Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create <strong>fields</strong> for <code>a</code> and <code>b</code>, and initialize them in <code>f()</code> with the parameters.</p> <p>E.g., like this:</p> <pre><code>public class CC{ private int a; private int b; private int d; private int c; public int f(int a,int b) { d=a; c=b; this.a = a; this.b = b; return a+b; } //want to use a &amp; b outside public int q(int a, int b) { return a+b; //re-initial-is- will delete the previous parameter value; } public int w(){ int p=a+b; //error:cannot access or resolve a &amp; b into variables return p; } public int e(){ int u=d+c; //works but this is not the solution return u; } public static void main(String args[]){ CC obj=new CC(); System.out.println(obj.f(3,4)); System.out.println(obj.q(8,9)); System.out.println(obj.w()); System.out.println(obj.e()); } } </code></pre> <p>This will print the following to the console:</p> <pre><code>7 17 7 7 </code></pre> <p>However, usage of the fields should depend on what you are trying to do.</p> <p>E.g., if you would want to use <em>a</em> and <em>b</em> once (for calculating <em>a</em> + <em>b</em>), then reset them with new values, and be able to use both the old and the new values, you would need to do something like this:</p> <pre><code>package test; public class CC { private int a; private int b; private int d; private int c; public int f(int initialA, int initialB) { this.a = initialA; this.b = initialB; return a+b; } //want to use a &amp; b outside public int q(int resetA, int resetB) { // Move values for a + b to other variables for re-use this.c = a; this.d = b; this.a = resetA; this.b = resetB; return a+b; //re-initial-is- will delete the previous parameter value; } public int w() { // Use the initial values int p = c + d; return p; } public int e() { // Use the reset values int u = a + b; //works but this is not the solution return u; } public static void main(String args[]){ CC obj = new CC(); System.out.println(obj.f(3,4)); System.out.println(obj.q(8,9)); System.out.println(obj.w()); System.out.println(obj.e()); } } </code></pre> <p>This will print the following to the console:</p> <pre><code>7 17 7 17 </code></pre>
 

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