Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: First time poster looking for help accessing a variable from a separate class.

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Thanked 0 Times in 0 Posts

    Default First time poster looking for help accessing a variable from a separate class.

    A quick introduction,
    I am very new to java programming, i have only taken an intro java course at my university. Since then i have been trying for the most part to teach myself. My way of doing this was to create a bare-bones black jack program and slowly build upon it as i learn. I had a working code, but it was sloppy and only used the main method. Since i've learned a little more about method calls, i've been trying to break my existing code down into organised classes and methods.

    With that said,
    the current issue i am running into now is implementing a variable that exists in another class. now i know i am supposed to create an object of a class before i can use it, but the class i am trying to call is already made into an object by "main" and creating a second object in a different class causes undesired effects.

    Keeping in mind that i am very new and have limited knowledge of programming, i would like to find a work around for this issue. I'm taking baby steps right now.

    Here's the code that is causing my issue (i can post more of the code if needed, also i will attach the source package).

    public class DealCard
        Deck  deck = new Deck();
        Random generator = new Random();
        boolean recieved;
        boolean pHasAce=false;
        boolean dHasAce=false;
        int dAce=0;
        int pAce=0;
        int card;
        int a=0;
        int b=0;
        boolean[] dealt = new boolean[52];
        public void dealDealer()
                recieved = false;
                while (!recieved)
                    card = generator.nextInt(51);                               
                    if (dealt[card]==false)
                        deck.dealerHandCard[a] = deck.deck[card];   
                        deck.dealerHandValue[a] = deck.value[card];
                        if (deck.value[card]==11)
    public class Printers
        public void printPlayerHandString()
            System.out.print("Player has ");
            for (int i=0; i<DealCard.b; i++)
                System.out.print(playerHandCard[i]+" ");


    black jack package.zip
    Last edited by royalcrown28; June 19th, 2012 at 11:26 PM.

  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Thanked 827 Times in 770 Posts
    Blog Entries

    Default Re: First time poster looking for help accessing a variable from a separate class.

  3. #3
    Join Date
    May 2010
    North Central Illinois
    My Mood
    Thanked 112 Times in 110 Posts

    Default Re: First time poster looking for help accessing a variable from a separate class.

    One way is to have setter and getter methods that return the variable. Another, less efficient, way is to simply make the variable public and call it using the class object.

    JavaClass jc = new JavaClass();




    Your DealCard.b isn't working as that only works that way with static variables, variables that are the same for all objects of that class.
    Last edited by javapenguin; June 19th, 2012 at 11:44 PM.

Similar Threads

  1. This Code worked on a separate class but does not work on another class
    By titowinky in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 25th, 2012, 08:48 AM
  2. accessing class method problem
    By steel55677 in forum Object Oriented Programming
    Replies: 11
    Last Post: February 23rd, 2012, 07:50 PM
  3. updating SWT Slider from separate java class
    By ppmckee in forum AWT / Java Swing
    Replies: 0
    Last Post: April 14th, 2011, 03:23 PM
  4. how to separate this code in another class
    By Jhovarie in forum AWT / Java Swing
    Replies: 2
    Last Post: February 28th, 2011, 05:22 PM
  5. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM