Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>How we can use a parameter variable outside the function without storing it into another variable?</p> </blockquote> <p>It's not possible. The scope of the local variables are confined to the method in which they are declared. All the local variables are stored on the stack frame for that method. When the method returns, the stack frame is destroyed, and thus all the local variables and parameters are lost.</p> <p>Quote from <a href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.6" rel="nofollow">JVM Specification - Frames</a>:</p> <blockquote> <p>A frame is used to store data and partial results, as well as to perform dynamic linking, return values for methods, and dispatch exceptions.</p> <p>A new frame is created each time a method is invoked. <strong>A frame is destroyed when its method invocation completes</strong>, whether that completion is normal or abrupt (it throws an uncaught exception). Frames are allocated from the Java Virtual Machine stack (§2.5.2) of the thread creating the frame. Each frame has its own array of local variables (§2.6.1), its own operand stack (§2.6.2), and a reference to the run-time constant pool (§2.5.5) of the class of the current method.</p> </blockquote> <hr /> <p>Having said that, it seems like <code>a</code> and <code>b</code> should really be fields of your class. And then you set them by either passing arguments to your constructor, or by setter methods. This is how you write an Object oriented code. Go through the <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/classes.html" rel="nofollow"><strong>Oracle tutorial on Classes</strong></a>.</p> <p>For e.g, take a simple class:</p> <pre><code>class Operation { private int a; private int b; public Operation(int a, int b) { this.a = a; this.b = b; } public int sum() { return a + b; } } </code></pre> <p>Now, you create an instance of this class by passing the arguments, that will be later on used in the <code>sum()</code> method:</p> <pre><code>Operation operation = new Operation(4, 5); System.out.println(operation.sum()); </code></pre> <p>You can modify your class along this line. And please choose a better name for your class and variables. Also follow standard Java naming conventions.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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