Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One of the fundamental concepts in OOP is the concept of scope: in almost all cases it is wise to reduce the scope of a variable (i.e. where it is visible from) to its minimum viable range.</p> <p>I'm going to assume you absolutely require the use of that variable in both functions. Therefore, the minimum viable scope in this case would cover both functions.</p> <pre><code>public class YourClass { private String yourStringVar; public int pleaseGiveYourFunctionProperNames(String s){ this.yourStringVar = s; } public void thisFunctionPrintsValueOfMyStringVar(){ System.out.println(yourStringVar); } } </code></pre> <p>Depending on the situation, you must assess the required scope of a variable, and you must understand the implications of increasing the scope (more access = potentially more dependencies = harder to keep track). </p> <p>As an example, let's say you absolutely needed it to be a GLOBAL variable (as you call it in your question). A variable with Global scope can be accessed by anything within the application. This is exceptionally dangerous, which I will demonstrate.</p> <p>To make a variable with global scope (there are no such things as global variables, exactly, in Java), you create a class with a static variable.</p> <pre><code>public class GlobalVariablesExample { public static string GlobalVariable; } </code></pre> <p>If I were to alter the original code, it would now look like this.</p> <pre><code>public class YourClass { public int pleaseGiveYourFunctionProperNames(String s){ GlobalVariablesExample.GlobalVariable = s; } public void thisFunctionPrintsValueOfMyStringVar(){ System.out.println(GlobalVariablesExample.GlobalVariable); } } </code></pre> <p>This can be exceptionally powerful, and exceptionally dangerous as it can lead to weird behaviour that you do not expect, and you lose many of the abilities that object oriented programming gives you, so use it carefully.</p> <pre><code>public class YourApplication{ public static void main(String args[]){ YourClass instance1 = new YourClass(); YourClass instance2 = new YourClass(); instance1.pleaseGiveYourFunctionProperNames("Hello"); instance1.thisFunctionPrintsValueOfMyStringVar(); // This prints "Hello" instance2.pleaseGiveYourFunctionProperNames("World"); instance2.thisFunctionPrintsValueOfMyStringVar(); // This prints "World" instance1.thisFunctionPrintsValueOfMyStringVar(); // This prints "World, NOT Hello, as you'd expect" } } </code></pre> <p>Always assess the minimum viable scope for your variables. Do not make it more accessible than it needs to be. </p> <p>Also, please don't name your variables a,b,c. And don't name your variables func1,func2. It doesn't make your application any slower, and it won't kill you to type in a few extra letters.</p>
    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. 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.
 

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