Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code looks very naive. A very simple answer to your question is </p> <p>You have not declared any cardNum in BankMainPart2 as global variable or in loginCard as local variable, how do you think it will be available in the loginCard method?</p> <pre><code>ArrayList&lt;Integer&gt; cardNum = new ArrayList&lt;Integer&gt;(); </code></pre> <p>is local to cardNumbers method.</p> <p>How can you access it from other class?</p> <ol> <li>A local variable cannot be accessed from outside the method, so first thing, make cardNum class level variable </li> <li>Make the variable public if you want other classes to be able to access it directly, else make the variable private and create getter method (setter if required).</li> <li>You can also send the variable when calling the method as argument </li> <li>If this is class level variable, make it static and use Classname.variable.</li> </ol> <p>--Edit-- </p> <p>As you have asked for details let me give you a quick overview of the different approaches.</p> <ol> <li>A variable declared inside a method is local. as name suggest "local", no one but the method knows there is such a variable. No other method in the class knows about existence of this variable, let alone some outside class.</li> <li>I say you can make it static, but static should strictly be used for class level storage, not object level. Say a list which is modified by multiple objects of the same class (I hope you know concepts of objects, else go to the basics otherwise it will not be clear). Now as per your example, I guess this is not what you want.</li> <li>A public variable is generally no - no, only in few cases it will be useful (for example in android programming where performance is utmost important). Normally we will create a variable and provide getter setters. A getter or setter is used normally when we want to give access to the variable, which again does not look like what you want.</li> <li>Last, the variable is private to you class, but if you want some method to do something about it, you can pass it as argument, this looks the case for you.</li> </ol> <p>Step by step </p> <p>take the variable out of method and add to class level, note that I removed static from method names </p> <pre><code>public class BankMain { private ArrayList&lt;Integer&gt; cardNum = new ArrayList&lt;Integer&gt;(); // rest of code as it is .. .. BankMain main = new BankMain(); //change main.menu(); //no need foe static public void cardNumbers(){ //no need here now //ArrayList&lt;Integer&gt; cardNum = new ArrayList&lt;Integer&gt;(); Scanner cards = new Scanner(System.in); Scanner input = new Scanner(System.in); .. .. //public static void menu(){ public void menu(){ //send the list //I see there are confusion at times regarding calling of static method. //please note objectname.staticMethod() or classname.staticMethod() is one //and same thing. Just that classname.staticMethod() is more clear BankMainPart2.loginCard(cardNum); } </code></pre> <p>and </p> <pre><code>public class BankMainPart2 { public static void loginCard(ArrayList&lt;Integer&gt; cardNum){ if (cardNum.contains(name)) { } } } </code></pre>
    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.
 

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