Results 1 to 4 of 4
  1. #1
    jocdrew21 is offline Member
    Join Date
    Jan 2014
    Posts
    64
    Rep Power
    0

    Default Assigning private variables values from constructor:

    I am able to get output from my constructor when I place a loop inside of it. However when I try to access the private variable that I thought was set by the constructor I get nothing. When I check its size it is zero as well.

    I am pretty confused.

    Java Code:
    public class WinningHand extends PokerCalculator
    {
    	private int p1Size;
    	private int p2Size;
    	private String[] p1Hand = new String[p1Size];
    	private String[] p2Hand = new String[p2Size];
    	
    	WinningHand()
    	{
    		
    		System.out.println(p1Size);
    		
    	}
    	WinningHand(ArrayList<String> p1, ArrayList<String> p2)
    	{
    		String[] player1 = new String[p1.size()]; 	
    		String[] player2 = new String[p2.size()];
    		
    		player1 = p1.toArray(player1);
    		player2 = p2.toArray(player2);
    		
    		this.p1Size= p1.size();
    		this.p2Size= p2.size();
    		
    		this.p1Hand = player1;
    		this.p2Hand = player2;
    	}
    Also posted at http://www.javaprogrammingforums.com...nstructor.html
    Last edited by jocdrew21; 06-13-2014 at 08:09 PM.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default Re: Assigning private variables values from constructor:

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    exonaut is offline Member
    Join Date
    Mar 2013
    Location
    USA
    Posts
    27
    Rep Power
    0

    Default Re: Assigning private variables values from constructor:

    Which constructor is getting called? And why are your constructors private?

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,775
    Rep Power
    5

    Default Re: Assigning private variables values from constructor:

    Quote Originally Posted by exonaut View Post
    Which constructor is getting called? And why are your constructors private?
    They're not. They're package-private. Which is just fine unless the OP wants to publish the class for general use.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 16
    Last Post: 01-08-2014, 06:02 PM
  2. Splitting String and assigning tokens to variables
    By igorland in forum New To Java
    Replies: 3
    Last Post: 02-06-2013, 02:47 AM
  3. Replies: 6
    Last Post: 09-21-2012, 09:12 PM
  4. Assigning array values to objects in a constructor
    By Dreaming in forum New To Java
    Replies: 8
    Last Post: 10-25-2011, 06:17 PM
  5. Assigning values to an object
    By camper2 in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 03:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •