Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:58:38+00:00 2026-06-11T15:58:38+00:00

I have 2 classes right now, the first class has the arraylist in it.

  • 0

I have 2 classes right now, the first class has the arraylist in it. But on the second class when I try to access the arraylist it keeps giving me the red line underneath saying that the variable doesn’t exist.

Here is class one…

public class BankMain {


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        BankMain main = new BankMain();

        menu();
    }

    public static void cardNumbers(){
        ArrayList<Integer> cardNum = new ArrayList<Integer>();
        Scanner cards = new Scanner(System.in);
        Scanner input = new Scanner(System.in);
        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please select a 5 digit card number");

        cardNum.add(input.nextInt());

        System.out.println("Thank you! You're card number is " +cardNum);

        System.out.println("Type 'c' to go back to main menu.");
        String value  = keyboard.next();
        if(value.equalsIgnoreCase("c")){
            menu();
        }
        else if (!keyboard.equals('c')){
            System.out.println("Invalid Entry!");
        }
    }

    public static void menu(){
        System.out.println("What Would you like to do today?");
        System.out.println();
        System.out.println("Create Account = 1");
        System.out.println("Login = 2");
        System.out.println("Exit = 3");
        query();
    }

    public static void query(){
        Scanner keyboard = new Scanner(System.in);
        double input = keyboard.nextInt();  

        if (input == 2){
            BankMainPart2 main2 = new BankMainPart2();
            System.out.println("Please enter your 5 digit card number.");
            main2.loginCard();
        }
        else if (input == 1){
            cardNumbers();
        }
        else if (input == 3){
            System.out.println("Thank you, have a nice day!");
            System.exit(0);
        }
    }
}

Here is the second class…

public class BankMainPart2 {
    public static void loginCard(){
        if (cardNum.contains(name)) {
        }
    }
}

I know I haven’t entered anything in the if statement yet on the second class but I’m just trying to get my array list to work on both classes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:58:39+00:00Added an answer on June 11, 2026 at 3:58 pm

    The code looks very naive. A very simple answer to your question is

    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?

    ArrayList<Integer> cardNum = new ArrayList<Integer>();
    

    is local to cardNumbers method.

    How can you access it from other class?

    1. A local variable cannot be accessed from outside the method, so first thing, make cardNum class level variable
    2. 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).
    3. You can also send the variable when calling the method as argument
    4. If this is class level variable, make it static and use Classname.variable.

    –Edit–

    As you have asked for details let me give you a quick overview of the different approaches.

    1. 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.
    2. 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.
    3. 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.
    4. 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.

    Step by step

    take the variable out of method and add to class level, note that I removed static from method names

    public class BankMain {
    private ArrayList<Integer> cardNum = new ArrayList<Integer>();
    // 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<Integer> cardNum = new ArrayList<Integer>();
            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);
    
    }
    

    and

    public class BankMainPart2 {
        public static void loginCard(ArrayList<Integer> cardNum){
            if (cardNum.contains(name)) {
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now, my project has two classes and a main. Since the two classes
I have a Java problem with nested classes. My first class structure looked like
I have two classes, A and B Class B has a private member variable
Here's what I have right now: public abstract class SampleClass { public abstract void
I have classes A, B and C. A has members B and C. C
I have classes structured like this: Public MustInherit Class A ' several properties End
Say I have classes class A{ //code for class A } class B{ //code
If I have classes of Type A and B: public class A { public
I have a two classes, a controller called AppController and a class called URLDelegate
In C++, I have an Object class, from which a number of other classes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.